场景:根据设备用途(字段为use_for)筛选查询设备信息,前端是下拉多选框,传给后台多个设备用途id(例如21 ,22);需要根据多个id查询出匹配的结果
方法1:
用mybatis遍历结合FIND_IN_SET 实现;以下list为用途id集合
select id,name,USE_FOR from device_basic_info where 1=1
<if test="list != null">
and
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")" separator="or">
FIND_IN_SET(#{item},USE_FOR)
</foreach>
</if>
方法2:
1.单独查询设备id为21的 设备信息
2.单独查询设备id为22的 设备信息
3.合并结果集并去除重复值
select distinct ,,dd.use_for from (
(select * from device_basic_info where FIND_IN_SET('21',USE_FOR)
UNION all
(select * from device_basic_info where FIND_IN_SET('22',USE_FOR)
) dd