[英]PHP with MySQL query not returning expected results
我是PHP的新手,正在尝试选择表中的记录(用于搜索功能),该表中的月份是固定的,而其他列则有所不同。
下面是查询:
SELECT *
FROM issue
WHERE month = "Apr 2019"
AND issue LIKE "%ekh%"
OR issue_type LIKE "%ekh%"
OR facility LIKE "%ekh%"
OR issue_id LIKE "%ekh%"
OR priority LIKE "%ekh%"
ORDER BY issue_id DESC
而是返回所有满足like子句的行,即使month isnt =“ Apr 2019”。
在代数中,“ AND
运算先于“ OR
运算。 这与您的WHERE
子句中的规则相同。
您可以使用如下括号来解决此问题:
SELECT *
FROM issue
WHERE month = "Apr 2019"
AND (issue LIKE "%ekh%"
OR issue_type LIKE "%ekh%"
OR facility LIKE "%ekh%"
OR issue_id LIKE "%ekh%"
OR priority LIKE "%ekh%")
ORDER BY issue_id DESC
当mysql首先命中时or
它将自动返回所有内容,因为or something is true
。 因此,您应该使用括号:
SELECT *
FROM issue
WHERE month = "Apr 2019" AND
(issue like "%ekh%" OR
issue_type like "%ekh%" OR
facility like "%ekh%" OR
issue_id like "%ekh%" OR
priority like "%ekh%")
ORDER BY issue_id DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.