gpt4 book ai didi

php - MYSQL HAVING 是否有替代方案

转载 作者:行者123 更新时间:2023-11-29 00:49:26 31 4
gpt4 key购买 nike

我正在尝试根据他们拥有的标签查找一些记录。为了找出一条记录有哪些标签,我使用子查询将它们添加到结果中。为了找出应该返回哪些结果,我在查询末尾添加了一个 having 语句。但有件事告诉我这不是最好的方法。

SELECT e.id, e.title, e.text, e.introduction,
UNIX_TIMESTAMP(e.starts_on) AS starts_on,
UNIX_TIMESTAMP(e.ends_on) AS ends_on,
m.id AS meta_id,
m.url,
cm.title AS category_title,
cm.url AS category_url,
CONCAT(
",",
(
SELECT GROUP_CONCAT(t.id)
FROM modules_tags AS mt
JOIN tags AS t ON t.id = mt.tag_id
WHERE mt.other_id = e.id
),","
) AS tags_search
FROM event_posts AS e
INNER JOIN meta AS m ON e.meta_id = m.id
LEFT JOIN event_categories AS c ON e.category_id = c.id
LEFT JOIN meta AS cm ON c.meta_id = cm.id
LEFT JOIN modules_tags AS mt ON mt.other_id = e.id
LEFT JOIN tags AS t ON t.id = mt.tag_id
WHERE 1 HAVING tags_search LIKE '%,5,%' AND tags_search LIKE '%,6,%'

最佳答案

FIND_IN_SET()是在 MySQL 中以逗号分隔值查找值的最佳方法。

关于php - MYSQL HAVING 是否有替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9175884/

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