日期:2014-05-16  浏览次数:20543 次

where not exists 与 if not exists 的区别
我相知道 if not exists  与where not exists 的区别

我是想判断是否有重复,没有重复就插入数据.
但有的数据并不是完全相同
例如,一个单子包含多个物品,需要更新库存.
这个时候要怎么处理呢?
------解决方案--------------------
if 是判断是否要执行下面的语句,where是对某个结果集或者表进行谓词逻辑的判断
------解决方案--------------------
我是想判断是否有重复,没有重复就插入数据.
但有的数据并不是完全相同
例如,一个单子包含多个物品,需要更新库存.
这个时候要怎么处理呢?

insert into tb (列名)
select 列名 from t a where exists (select 1 from tb b where a.主键<>b.主键)--如果没有主键,就在内层where中把需要判断相同的列写全
------解决方案--------------------
你这种情况不建议使用if那个,一般来说常用于判断复合条件的数据是否存在,比如:
if not exists  (select 1 from tb where xxxx)--表TB中符合where条件的数据是否存在,如果存在,执行下面语句,如果不存在,执行else的语句
begin

end
else 
begin
   
end
------解决方案--------------------
if not exists是用来判断程序逻辑的,也就是在存储过程中,通过这个来判断程序流程。
where exists是用来从筛选数据的,例如查找A表中ID存在于B表中的数据