我是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子句的所有行,即使月份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