insert 与 select 的联合使用

下面介绍Mysql和Sqlite和Sqlserver中,根据select的条件判断是否插入。例如:

一、Mysql中:

INSERT INTO books (name) SELECT 'msg' FROM dual WHERE NOT EXISTS (SELECT id FROM books WHERE id = 1)

 

二、Sqlite中:

由于Sqlite中没有临时表:dual

所以,需要这样写

 INSERT INTO books (name)  SELECT 'msg'  WHERE NOT EXISTS (SELECT id FROM books WHERE id = 1)

三、Sqlserver中:

SqlServer中需要另一种写法:

IF NOT EXISTS (SELECT id FROM books WHERE id = 1)  INSERT INTO books (name) SELECT 'msg'

 

 

上一篇:站得高,才能尿得远


下一篇:计算机中减法的实现原理