赞
踩
方法一:
1、mysql查询
SELECT
user_name,
user_type
FROM
user
WHERE
find_in_set('1', user_type)
OR find_in_set('2', user_type)
2、程序里mybatis可以这样写
传参:List 类型 userTypeList:[1,2]
<select id="" parameterType="" resultMap="">
SELECT user_name,user_type
FROM user
<where>
1=1
<if test="userTypeList != null and userTypeList.size() >0">
<foreach collection="userTypeList" open="and (" close=")" item="id" separator="or">
<if test="id != null and id != '' ">
find_in_set(#{id}, user_type)
</if>
</foreach>
</if>
</where>
</select>
方法二:
1、mysql查询
SELECT
user_name,
user_type
FROM
user
WHERE 1=1
AND CONCAT(',', user_type, ',') REGEXP '(,2,)|(,1,)'
(,2,)作为一个整体查询,这样处理是因为REGEXP不能根据逗号 分隔筛选,会查出来所有带2的角色,如:21
2、程序里写法
传参:String类型 userType: “1,2”
<select id="" parameterType="" resultMap="">
SELECT user_name,user_type
FROM user
<where>
1=1
<if test="userType != null and userType != '' ">
AND CONCAT(',', user_type, ',') REGEXP (CONCAT('(,', REPLACE(#{userType},',',',)|(,'), ',)'))
</if>
</where>
</select>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。