MySQL多列搜索返回错误结果

时间:2017-03-01 09:17:48

标签: mysql search conditional multiple-columns

我在条件查询下方执行多列搜索

SELECT 
    prod.pid, 
    prod.pname, 
    prod.description, 
    prod.status, 
    foto.set_cover, 
    foto.filename
FROM products prod
INNER JOIN second_child_categories secd ON prod.category_id = secd.second_id
INNER JOIN photos foto ON prod.pid = foto.pid
WHERE CONCAT( prod.pname, prod.description ) LIKE '%home%'
OR CONCAT( prod.description, prod.pname ) LIKE '%home%'
AND foto.set_cover = '1'
AND prod.status = '1'
ORDER BY prod.created_date DESC 

这是输出:

enter image description here

参考查询,我设置条件:

foto.set_cover='1' AND prod.status='1'

结果与条件不符。

如何修复此查询?

感谢。

1 个答案:

答案 0 :(得分:0)

请参阅Mysql or/and precedence?

OR具有更高的优先级,你(隐含地)写道:

CONCAT( prod.pname, prod.description ) LIKE '%home%'
OR (
  CONCAT( prod.description, prod.pname ) LIKE '%home%'
  AND foto.set_cover = '1'
  AND prod.status = '1'
)

我认为你的意思

( 
  CONCAT( prod.pname, prod.description ) LIKE '%home%'
  OR 
  CONCAT( prod.description, prod.pname ) LIKE '%home%'
)
AND foto.set_cover = '1'
AND prod.status = '1'