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 匹配的数量来排序项目?我深入手册,找不到答案,迷失在我不理解的东西中。

4

1 回答 1

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
于 2013-02-12T19:47:19.180 回答