码迷,mamicode.com
首页 > 数据库 > 详细

mysql根据关键词查询匹配多个字段时结果不正确

时间:2019-09-21 17:00:26      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:tin   catalog   rom   concat   col   log   增加   resource   abstract   

一开始的写法

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>

 

mysql根据关键词查询匹配多个字段时结果不正确

标签:tin   catalog   rom   concat   col   log   增加   resource   abstract   

原文地址:https://www.cnblogs.com/cailijuan/p/11563365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!