簡體   English   中英

PHP與MySQL查詢未返回預期結果

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM