如何获得MySQL中每个标签最常出现的类别?理想情况下,我想模拟一个聚合函数来计算列的模式。
SELECT t.tag , s.category FROM tags t LEFT JOIN stuff s USING (id) ORDER BY tag; +------------------+----------+ | tag | category | +------------------+----------+ | automotive | 8 | | ba | 8 | | bamboo | 8 | | bamboo | 8 | | bamboo | 8 | | bamboo | 8 | | bamboo | 8 | | bamboo | 10 | | bamboo | 8 | | bamboo | 9 | | bamboo | 8 | | bamboo | 10 | | bamboo | 8 | | bamboo | 9 | | bamboo | 8 | | banana tree | 8 | | banana tree | 8 | | banana tree | 8 | | banana tree | 8 | | bath | 9 | +-----------------------------+
SELECT t1.* FROM (SELECT tag, category, COUNT(*) AS count FROM tags INNER JOIN stuff USING (id) GROUP BY tag, category) t1 LEFT OUTER JOIN (SELECT tag, category, COUNT(*) AS count FROM tags INNER JOIN stuff USING (id) GROUP BY tag, category) t2 ON (t1.tag = t2.tag AND (t1.count < t2.count OR t1.count = t2.count AND t1.category < t2.category)) WHERE t2.tag IS NULL ORDER BY t1.count DESC;
我同意这对于单个SQL查询来说实在太多了。GROUP BY子查询内部的任何使用都会使我畏缩。您可以通过使用视图使它 看起来 更简单:
GROUP BY
CREATE VIEW count_per_category AS SELECT tag, category, COUNT(*) AS count FROM tags INNER JOIN stuff USING (id) GROUP BY tag, category; SELECT t1.* FROM count_per_category t1 LEFT OUTER JOIN count_per_category t2 ON (t1.tag = t2.tag AND (t1.count < t2.count OR t1.count = t2.count AND t1.category < t2.category)) WHERE t2.tag IS NULL ORDER BY t1.count DESC;
但它基本上是在幕后做同样的工作。
您评论说,您可以在应用程序代码中轻松地执行类似的操作。那你为什么不那样做呢?进行更简单的查询以获取每个类别的计数:
SELECT tag, category, COUNT(*) AS count FROM tags INNER JOIN stuff USING (id) GROUP BY tag, category;
并在应用程序代码中对结果进行排序。