繁体   English   中英

MySQL中分组行的排序

[英]Ordering of grouped rows in mysql

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM