簡體   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