gpt4 book ai didi

mysql - mysql 中分组行的排序

转载 作者:行者123 更新时间:2023-11-29 14:02:36 25 4
gpt4 key购买 nike

SELECT t1.item_id, t1.item, t1.created, t1.owner, t1.type, t1.fld, t2.tag
FROM items AS t1
INNER JOIN tagged AS t2 ON t1.item_id = t2.item_id
WHERE tag
IN ('how', 'jkas', 'bodor', 'zimp', 'ctuo', 'sjex', 'kek'
)
GROUP BY item_id
ORDER BY t1.created DESC
LIMIT 0 , 100

如何根据商品与 IN TAG 的匹配数量来排序?我非常深入地研究了手册,但找不到答案,并且迷失在我不理解的东西中。

最佳答案

您可以使用COUNT并将结果放入子查询中:

SELECT * 
FROM (
SELECT t1.item_id, t1.item, t1.created, t1.owner,
t1.type, t1.fld, t2.tag, COUNT(DISTINCT t2.tag) tagCount
FROM items AS t1
INNER JOIN tagged AS t2 ON t1.item_id = t2.item_id
WHERE tag IN ('how', 'jkas', 'bodor', 'zimp', 'ctuo', 'sjex', 'kek')
GROUP BY item_id
) t
ORDER BY tagCount DESC, created DESC
LIMIT 0 , 100

关于mysql - mysql 中分组行的排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14840491/

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