场景:
使用子查询搜索不到数据,子查询搜索到的数据: 10,11
select
*
from
sys_user_info where blng_dept_id in (10,11)
and use_state ='1'
直接将数据插进去却可以。
主要原因:
select
*
from
sys_user_info where blng_dept_id in ("10,11")
and use_state ='1'
由于搜的数据是字符串导致in方法无效。
解决方法:
1.mysql find_in_set(数据库对应字段,子查询sql)函数
比如说: id是某一个表的主键字段,然后每条记录分别是id等于1,2,3,4,5的时候, 就有点类似in() 的操作了
select * from per where find_in_set(id,'2,3,4,5,6');
等同于
select * from per where id in (2,3,4,5,6)
2.写成两个sql进行查询,将子查询获得的数据通过split分割成数组,Arrays.asList(str)成集合,使用批量方法中常用的
<foreach collection="deptNos" item="deptNo" open="and blng_dept_id in (" separator="," close=")">
#{deptNo}
</foreach>
注意使用<where>或 1=1 动态sql拼接问题及@Param的使用。