SQL NOT IN 函数
我正在尝试插入一条记录,并且我想检查它是否已存在于表中。
我尝试了
INSERT INTO emp (empno, name)
VALUES(2, 'ram')
WHERE empno NOT IN (select empno from emp);
,但它显示错误“附近语法不正确
”
Iam trying to insert a record and i want to check that it is not already present in the table.
I try
INSERT INTO emp (empno, name)
VALUES(2, 'ram')
WHERE empno NOT IN (select empno from emp);
but it shows error 'incorrect syntax near where
'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用以下查询将记录插入 emp
如果您一次插入一条记录,那么下面的查询将尽可能地工作...
如果您愿意插入多条记录,那么只需找到下面的查询
You can use following query to insert records into emp
If you are inserting one record at a time then following query will work as best as it can ...
If you are willing to insert multiple records then just find below query
INSERT
语句上不能有WHERE
子句,只能在SELECT/UPDATE
上有WHERE
子句如果您使用 MySQL,您可以执行以下操作:
如果您在 name 列上有一个
unique
索引,那么您将是安全的You can't have
WHERE
clause onINSERT
statements, you haveWHERE
clause only withSELECT/UPDATE
If you work with MySQL, you could do something like:
And if you have a
unique
index on name column, you will be safe如果该行存在,您可以使用 INSERT IGNORE 来静默失败。它将尝试插入,但如果密钥存在,它将不执行任何操作。
您可能还想看看
INSERT ...关于重复密钥更新
You can use
INSERT IGNORE
to fail silently if the row exists. It will attempt the insert, but if the key exists it will do nothing.You might also want to take a look at
INSERT ... ON DUPLICATE KEY UPDATE
你可能会寻找类似的东西
???
you'd probably be looking for something like
???