Sqlite、MySQL和SQL Server中INSERT ... SELECT ... WHERE NOT EXISTS的用法

Sqlite、MySQL和SQL Server中INSERT … SELECT … WHERE NOT EXISTS的用法

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

一、Mysql中:

INSERT INTO books (name) SELECT ‘SongXingzhu’ FROM dual WHERE NOT EXISTS
(SELECT id FROM books WHERE id = 1)

二、Sqlite中:

由于Sqlite中没有临时表:dual

所以,需要这样写

INSERT INTO books (name) SELECT ‘Songxingzhu’ 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 ‘Songxingzhu’


Sqlite、MySQL和SQL Server中INSERT ... SELECT ... WHERE NOT EXISTS的用法
https://www.dearcloud.cn/2017/12/20/20200310-cnblogs-old-posts/20171220-Sqlite和Mysql和SqlServer中insert…select…wherenotexist的用法/
作者
宋兴柱
发布于
2017年12月20日
许可协议