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