postgresql - 不存在的 PostgreSQL 重复值

标签 postgresql sql-insert not-exists

我写了这段代码:

INSERT into author(authorfirstname1, authorlastname1,authorfirstname2, authorlastname2)
select '".addslashes($_POST['authorfirstname1'])."','".addslashes($_POST['authorlastname1'])."','".addslashes($_POST['authorfirstname2'])."','".addslashes($_POST['authorlastname2'])."'
from author
where not exists(select authorfirstname1, authorlastname1, authorfirstname2, authorlastname2 from author
where author.authorfirstname1='".addslashes($_POST['authorfirstname1'])."'
and author.authorlastname1='".addslashes($_POST['authorlastname1'])."'
and author.authorfirstname2='".addslashes($_POST['authorfirstname2'])."'
and author.authorlastname2='".addslashes($_POST['authorlastname2'])."'
);

这段代码的重点应该是检查数据库中是否已经存在一个值,如果不存在,则将其输入。 这个 '".addslashes($_POST['authorlastname2'])."' 表示输入,但可以很容易地替换为 '%myentereddata%'

我的问题是它什么也没做...甚至不给出错误消息,它成功了,但是如果数据不存在于数据库中它就不会输入数据,并且不确定如果数据存在它是否停止输入数据。

因此,如果有人可以帮助我解决此代码的问题或给出另一个示例如何以不同的方式进行操作,我将不胜感激。

我的id是primary和serial的,不用插入

INSERT INTO author (authorfirstname1, authorlastname1,authorfirstname2, authorlastname2)
VALUES ('one','two','three','four');

查询成功返回:1 行受影响,执行时间 60 毫秒。

INSERT into author(authorfirstname1, authorlastname1,authorfirstname2, authorlastname2)
select 'one','two','three','four'
from author
where not exists(select authorfirstname1, authorlastname1, authorfirstname2, authorlastname2 from author
where author.authorfirstname1='one'
and author.authorlastname1='two'
and author.authorfirstname2='three'
and author.authorlastname2='four'
);

查询成功返回:657 行受影响,执行时间 40 毫秒。

INSERT into author(authorfirstname1, authorlastname1,authorfirstname2, authorlastname2)
select 'new','new','new','new'
from author
where not exists(select authorfirstname1, authorlastname1, authorfirstname2, authorlastname2 from author
where author.authorfirstname1='new'
and author.authorlastname1='new'
and author.authorfirstname2='new'
and author.authorlastname2='new'
);

查询成功返回:1314 行受影响,执行时间 70 毫秒。

最佳答案

问题是有两个 FROM 子句而不是一个。实际上,您插入同一行的次数与整个表中的行数一样多(当满足 WHERE 子句时)。 查询应该是(请注意,与您的版本相比,第一个 FROM 已被删除):

INSERT into author(authorfirstname1, authorlastname1,authorfirstname2, authorlastname2)
select 'one','two','three','four'
where not exists(select 1 from author
where author.authorfirstname1='one'
and author.authorlastname1='two'
and author.authorfirstname2='three'
and author.authorlastname2='four'
);

内部选择中的列也已被删除。 SELECT 1 FROM... 足以检查一行是否存在,不需要提取特定的列,它们无论如何都会在上层被丢弃。

另一个不相关的问题是,只要任何注入(inject)的参数包含引号字符,使用 addslashes 完成的转义将产生无效查询。

这是因为当PG的standard_conforming_strings设置为ON时,反斜杠是一个普通字符,不会转义任何东西。从 PostgreSQL 9.1 开始,默认情况下它是打开的。

改用pg_escape_string

关于postgresql - 不存在的 PostgreSQL 重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16734832/

相关文章:

ruby-on-rails - 无法使用 Postgres.app 在 Mavericks 中安装 pg gem

mysql - 如何将返回多行的 SELECT 语句与 INSERT 语句组合起来?

sql - 从sql中的点数据库中获取路径

sql - 不兼容的类型 : numeric and bigint. 在 PostgreSQL 中

c# - Entity Framework Core : Npgsql. PostgresException: 23505: 重复键值违反唯一约束

sql - 从/向数据库删除+插入数据

mysql - 插入带有来自 Select 的值的查询

Mysql IF NOT EXISTS then 语法

MySQL 过程检查记录是否在插入不工作之前存在

Django 迁移依赖引用不存在的父节点