2013-02-12 115 views
0
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匹配的次數來訂購商品?我的手冊非常深入,我找不到答案,我迷失在我不明白的東西中。在mysql中對分組行進行排序

回答

1

您可以使用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 
+1

謝謝。我爲你感到驕傲。 – user2044390 2013-02-12 19:48:30

+0

@ user2044390 - 我甚至沒有打字:)讓我知道如果你有任何問題 - 非常未經測試。祝你好運。 – sgeddes 2013-02-12 19:49:35

+0

問題出現了#1248 - 每個派生表都必須有自己的別名 – user2044390 2013-02-12 19:51:39