场景:

使用子查询搜索不到数据,子查询搜索到的数据: 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的使用。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐