SQL NOT IN 函数

发布于 2024-12-18 13:39:45 字数 205 浏览 2 评论 0原文

我正在尝试插入一条记录,并且我想检查它是否已存在于表中。

我尝试了

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玻璃人 2024-12-25 13:39:45

您可以使用以下查询将记录插入 emp
如果您一次插入一条记录,那么下面的查询将尽可能地工作...

insert into emp (empno,empname)
    select distinct empno,empname
      from ( 2 empno, 'ram' empname ) as a 
     where  a.empname not in ( select empname from emp )

如果您愿意插入多条记录,那么只需找到下面的查询

insert into emp (empno,empname)
    select max(empno),empname 
      from ( select 2 empno, 'ram' empname 
              union 
             select 3 empno, 'ram1' empname 
              union 
             select 4 empno, 'ram' empname 
           ) as a 
     where a.empname not in ( select empname from emp )
     group by empname 

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 ...

insert into emp (empno,empname)
    select distinct empno,empname
      from ( 2 empno, 'ram' empname ) as a 
     where  a.empname not in ( select empname from emp )

If you are willing to insert multiple records then just find below query

insert into emp (empno,empname)
    select max(empno),empname 
      from ( select 2 empno, 'ram' empname 
              union 
             select 3 empno, 'ram1' empname 
              union 
             select 4 empno, 'ram' empname 
           ) as a 
     where a.empname not in ( select empname from emp )
     group by empname 
动听の歌 2024-12-25 13:39:45

INSERT 语句上不能有 WHERE 子句,只能在 SELECT/UPDATE 上有 WHERE 子句

如果您使用 MySQL,您可以执行以下操作:

insert into emp (empno, name) values(2, 'ram') ON DUPLICATE KEY UPDATE name = 'ram'

如果您在 name 列上有一个 unique 索引,那么您将是安全的

You can't have WHERE clause on INSERT statements, you have WHERE clause only with SELECT/UPDATE

If you work with MySQL, you could do something like:

insert into emp (empno, name) values(2, 'ram') ON DUPLICATE KEY UPDATE name = 'ram'

And if you have a unique index on name column, you will be safe

旧时模样 2024-12-25 13:39:45

如果该行存在,您可以使用 INSERT IGNORE 来静默失败。它将尝试插入,但如果密钥存在,它将不执行任何操作。

INSERT IGNORE INTO emp (empno, name) VALUES (2, 'ram')

您可能还想看看 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.

INSERT IGNORE INTO emp (empno, name) VALUES (2, 'ram')

You might also want to take a look at INSERT ... ON DUPLICATE KEY UPDATE

舂唻埖巳落 2024-12-25 13:39:45

你可能会寻找类似的东西

insert into emp (empno, name) 
SELECT 2 , 'ram'
FROM emp 
WHERE 2 not in (select empno from emp) 

???

you'd probably be looking for something like

insert into emp (empno, name) 
SELECT 2 , 'ram'
FROM emp 
WHERE 2 not in (select empno from emp) 

???

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文