0

我有 3 张桌子 - News, Content and Edits

每个表中的每一行都包含一个user_id字段。

我想做的是列出这 3 个表的主要贡献者。

所以我需要计算user_id每个表中每个不同出现的次数,并从最高计数到最低计数。

4

3 回答 3

3

像这样:

SELECT 
    user_id,
    COUNT(*) AS all_actions 
FROM (
    SELECT user_id FROM News
    UNION ALL
    SELECT user_id FROM Content
    UNION ALL
    SELECT user_id FROM Edits
) tmp_table
GROUP BY user_id
ORDER BY all_actions DESC
于 2012-12-14T20:54:50.453 回答
0

这应该这样做。

SELECT all_user_ids.user_id as `user_id`, COUNT(all_user_ids.user_id) as `count`
FROM
(SELECT user_id FROM news
UNION ALL
SELECT user_id FROM content
UNION ALL
SELECT user_id FROM edits) AS all_user_ids
GROUP BY `user_id`
ORDER BY `count` DESC
于 2012-12-14T20:56:00.837 回答
0

这将是每个表的实例

SELECT DISTINCT(COUNT(user_id)) as num FROM news GROUP BY user_id ORDER BY num DESC LIMIT 0,3
于 2012-12-14T20:57:03.713 回答