sql中exists进行多列的判断

in和not exists都是都可以。如下sql:

-- 注意in 后面的内容要用括号() 括起来
select * from b where (aaa,bbb) not in ( select aaa,bbb from a where 查询条件A);

select * from b where not exists ( select * from a where a.aaa=b.aaa and a.bbb=b.bbb and 查询条件A);

in返回的结果和条件顺序一致么

不一定,会按照返回结果字段进行排序。
所以使用的时候,一定不要看in的顺序,就认为结果也是这顺序,最好带标识字段。

in好还是 !=好

要看情况。 例如0是正常。 1,2,3,4,5都是错误
很显然 != 0 比较简单。 以后如果6也要过滤。 加个条件 and !=6 即可。
但是in的条件较少,如 in (1) ,那么!= 反而麻烦。

互逆条件的处理

代码:

<if test='params.isUse == "1" '>
    where exists(
    select 1
    from t
    where t.inv_num is not null
    )
</if>
<if test='params.isUse == "0" '>
    where not exists(
    select 1
    from t
    where t.inv_num is not null
    )
</if>

注:互逆条件,一个是exists,一个用not exists。
但是where中的小条件要一致,如果条件也互逆那么就错了。

例如,错误代码:

<if test='params.isUse == "1" '>
    where exists(
    select 1
    from t
    where t.inv_num is not null
    )
</if>
<if test='params.isUse == "0" '>
    where not exists(
    select 1
    from t
    where t.inv_num is null   --  这就错了
    )
</if>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值