一、将10,20,30,40,50(逗号拼接)字段拆开并查询多状态值
1.1通过sql内置函数
注意:
(1)将projects替换成查询的表
(2)将related_contentid替换成指定字段
(3)UNION ALL SELECT 必须指定到最大拼接长度
mysql语句
SELECT
*
FROM
projects
CROSS JOIN
( SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 ) numbers
WHERE
TRIM(
SUBSTRING_INDEX( SUBSTRING_INDEX( related_contentid, ',', numbers.n ), ',', - 1 )) IN ( 10, 25, 35 )
AND numbers.n <= LENGTH( related_contentid ) - LENGTH(
REPLACE ( related_contentid, ',', '' )) + 1;
xml方式
<if test="filterQO.bsrangeArray != null and filterQO.bsrangeArray.length > 0">
AND id in (
SELECT id FROM projects
CROSS JOIN ( SELECT 1 AS n UNION ALL SELECT 2 UNION ALL SELECT 3 ) numbers
WHERE
TRIM(SUBSTRING_INDEX( SUBSTRING_INDEX( bsrange_id, ',', numbers.n ), ',', - 1 )) IN
<foreach collection="filterQO.bsrangeArray" index="index"
open="(" item="item" separator="," close=")">
#{item}
</foreach>
AND LENGTH( bsrange_id ) - LENGTH(REPLACE ( bsrange_id, ',', '' )) + 1 >= numbers.n)
</if>
1.2 通过查询获取
注意:
(1)将projects替换成查询的表
(2)bsrange_id
sql语句
SELECT
*
FROM
projects
WHERE
( bsrange_id like concat('%,','80',',%')
or bsrange_id like concat('%,','80','')
or bsrange_id like concat('','80',',%')
or bsrange_id = '80' )
xml
<if test="filterQO.bsrangeArray != null and filterQO.bsrangeArray.length > 0">
AND
<foreach collection="filterQO.bsrangeArray" index="index" open="(" item="item" separator="or" close=")">
bsrange_id like concat('%,',#{item},',%')
or bsrange_id like concat('%,',#{item},'')
or bsrange_id like concat('',#{item},',%')
or bsrange_id = #{item}
</foreach>
</if>