mysql中exists、not exists的用法

exists 关键字是判断是否存在的,存在则返回true,不存在则返回false, not exists则是不存在时返回true,存在返回false:

1. 最常用的if not  exists用法: create  table    if not exists   AA  如果表AA不存在(返回true)则创建表

2. select 语句中使用exists, 如:

     select a.id,a.name  from user  where  exists (select *  from class  where a.class_id = c.class_id)

3. insert into中使用not exists, 为了表中不插入重复语句,可以在insert into 中使用not exists进行判断,如果不存在相同的记录(not exists返回true)则插入,否则不执行,语句如下:  

insert into  tableA(id,name,start_time,ds)
select '1234','testName','2017-07-01 18:00:00','20170701 ' from dual WHERE not exists (select * from tableA
where instance_id = '1234' );

 

   

 

posted @ 2017-07-11 13:59  归去也无风雨也无晴  阅读(11095)  评论(0编辑  收藏  举报