mysql根据关键词查询匹配多个字段时结果不正确
一开始的写法
SELECT rrc.id, rrc.resource_name rrc.is_publish FROM res_resource_catalog AS rrc <where> <if test="keyWord != null and keyWord != ''"> AND rrc.resource_name LIKE concat(concat("%",#{keyWord}),"%") OR rrc.resource_abstract LIKE concat(concat("%",#{keyWord}),"%") OR rrc.id IN (SELECT DISTINCT resource_id FROM res_info_item WHERE item_name LIKE concat(concat("%",#{keyWord}),"%")) </if> AND rrc.is_publish = 1 </where>
正确的写法:在<if>里面增加一对()
SELECT rrc.id, rrc.resource_name rrc.is_publish FROM res_resource_catalog AS rrc <where> <if test="keyWord != null and keyWord != ''"> AND (rrc.resource_name LIKE concat(concat("%",#{keyWord}),"%") OR rrc.resource_abstract LIKE concat(concat("%",#{keyWord}),"%") OR rrc.id IN (SELECT DISTINCT resource_id FROM res_info_item WHERE item_name LIKE concat(concat("%",#{keyWord}),"%"))) </if> AND rrc.is_publish = 1 </where>