gpt4 book ai didi

php - 来自带有 LIKE 子句的类别的 MySQL 请求

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:31 25 4
gpt4 key购买 nike

我在下面有一个 MySQL 查询。我的 q.catid = 13 (category_id) 但此查询返回其他类别的结果。我的错误在哪里?

SELECT q.*, u.name, u.username, u.email, c.title AS category_name, COUNT(a.id) AS answers 
FROM `#__questions` AS q
LEFT JOIN `#__answers` AS a ON a.question_id = q.id
LEFT JOIN `#__users` AS u ON q.created_by = u.id
LEFT JOIN `#__categories` AS c ON q.catid = c.id
WHERE 1=1
AND q.catid = 13
AND q.title LIKE "%3%"
OR q.introtext LIKE "%3%"
GROUP BY q.id
ORDER BY q.created DESC

最佳答案

因为 and 的绑定(bind)比 or 强。这称为运算符优先级

使用括号

WHERE 1=1 
AND q.catid = 13
AND (q.title LIKE "%3%" OR q.introtext LIKE "%3%")

没有括号,您的查询当前解析为

WHERE (1=1 AND q.catid = 13 AND q.title LIKE "%3%")
OR q.introtext LIKE "%3%"

关于php - 来自带有 LIKE 子句的类别的 MySQL 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25508894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com