php - Mysql 在同一查询中使用 AND & OR LIKE 时排除第一个参数?

标签 php mysql

我正在尝试执行这个非常简单的查询。

links_public 1 = show

links_public 0 = dont show

但是,当运行下面的查询时,它仍然返回所有 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

最佳答案

尝试使用一些括号。

有点像

 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

关于php - Mysql 在同一查询中使用 AND & OR LIKE 时排除第一个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16712270/

相关文章:

php - 我如何将逻辑与表示分开?

php - 如何在有条件的地方使用 Count by laravel eloquent

javascript - jquery 动态元素滚动

php - Dbforge 在 CodeIgniter 中导致错误编号 1064

php - 连接 MySQL 时出现访问被拒绝错误

python - 如何使用 mysql.connector 从 MySQL 返回 str?

Mysql更新两个表不工作

Mysql查询多条件错误

mySQL 查询 : involving 2 tables

PHP - 这是 PHP 还是 CentOS、Red hat 企业 linux 错误?做php时://output it fails to output under Red hat linux but works in other OS