带有MySQL查询的PHP没有返回预期的结果

问题描述 投票:2回答:2

我是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”也是如此。

php mysql sql
2个回答
2
投票

在代数中,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

1
投票

当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
© www.soinside.com 2019 - 2024. All rights reserved.