0

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

links_public 1 = 显示

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

4 回答 4

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。运算符优先级

于 2013-05-23T11:06:55.967 回答
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
于 2013-05-23T11:06:49.913 回答
1

手术室正在制造问题。您需要使用 () 对条件进行分组。你应该使用: -

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
于 2013-05-23T11:08:11.283 回答
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
于 2013-05-23T11:07:11.360 回答