dtxa49711 2019-04-09 21:51
浏览 84
已采纳

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

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.

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".

  • 写回答

2条回答 默认 最新

  • dongqiaolong9034 2019-04-09 21:56
    关注

    In algebra, the AND operation is done before the OR operation. This is the same rule in your WHERE clause.

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?