简体   繁体   English

PHP与MySQL查询未返回预期结果

[英]PHP with MySQL query not returning expected results

I'm new to PHP and I'm trying to select records (for search functionality) in my table where the month is static and other columns vary. 我是PHP的新手,正在尝试选择表中的记录(用于搜索功能),该表中的月份是固定的,而其他列则有所不同。

Below is the query: 下面是查询:

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

This is rather returning all rows that satisfy the like clauses, even when month isnt = "Apr 2019". 而是返回所有满足like子句的行,即使month isnt =“ Apr 2019”。

In algebra, the AND operation is done before the OR operation. 在代数中,“ AND运算先于“ OR运算。 This is the same rule in your WHERE clause. 这与您的WHERE子句中的规则相同。

You can fix this with parentheses like this : 您可以使用如下括号来解决此问题:

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

When mysql hits first or it automatically returns everything because or something is true . 当mysql首先命中时or它将自动返回所有内容,因为or something is true Therefore you should use parentheses: 因此,您应该使用括号:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM