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>