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' );

 

   

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM