使用AND&时,Mysql排除第一个参数或者在同一个查询中?

时间:2013-05-23 11:04:35

标签: php mysql

我试图执行这个非常简单的查询。

  

links_public 1 = show

     

links_public 0 =不显示

但是,当运行下面的查询时,它仍会返回links_public设置为0的所有行。

 SELECT *
 FROM links
 WHERE links_public = '1'
 AND links_description LIKE '%$filter%'
 OR links_created_by LIKE '%$filter%'
 OR links_link LIKE '%$filter%'
 ORDER BY links_created DESC

4 个答案:

答案 0 :(得分:4)

尝试使用一些括号。

这样的东西
 SELECT *
 FROM links
 WHERE links_public = '1'
 AND (links_description LIKE '%$filter%'
 OR links_created_by LIKE '%$filter%'
 OR links_link LIKE '%$filter%')
 ORDER BY links_created DESC

你拥有它的方式,它被视为

SELECT *
 FROM links
 WHERE (links_public = '1'
 AND links_description LIKE '%$filter%')
 OR links_created_by LIKE '%$filter%'
 OR links_link LIKE '%$filter%'
 ORDER BY links_created DESC

查看12.3.1. Operator Precedence

答案 1 :(得分:2)

更改

SELECT *
 FROM links
 WHERE links_public = '1'
 AND links_description LIKE '%$filter%'
 OR links_created_by LIKE '%$filter%'
 OR links_link LIKE '%$filter%'
 ORDER BY links_created DESC

SELECT *
 FROM links
 WHERE links_public = '1'
 AND ( links_description LIKE '%$filter%'
 OR links_created_by LIKE '%$filter%' OR links_link LIKE '%$filter%' )
 ORDER BY links_created DESC

答案 2 :(得分:1)

OR正在创建问题。您需要使用()来分组条件。你应该使用: -

SELECT *
 FROM links
 WHERE links_public = '1'
 AND (links_description LIKE '%$filter%'
 OR links_created_by LIKE '%$filter%' OR links_link LIKE '%$filter%' )
 ORDER BY links_created DESC

答案 3 :(得分:0)

放置括号,然后尝试:

SELECT *
 FROM links
 WHERE (links_public = '1')
 AND (links_description LIKE '%$filter%'
 OR links_created_by LIKE '%$filter%'
 OR links_link LIKE '%$filter%')
 ORDER BY links_created DESC