SQL使用COUNT()和LEFT JOIN合并两个或三个表

时间:2014-01-25 13:31:25

标签: mysql sql

我确定之前已经问过这个问题,但我无法让我的代码工作:(

用户提出了多个问题。我需要显示用户进行有点用户搜索,然后显示每个用户提出的问题数量。

表1(用户): ID, 用户名, 密码, 化身, reg_date, is_banned(ETC ...)

表2(问题): ID, questioner_id, 题, 回答1, 回答2, 回答3(ETC ......)

我需要将两者合并,然后显示普通用户信息,例如用户名等,还要计算questioner_id行并显示用户提出的问题数量。

这是我到目前为止所做的,但它只是吐出一个结果并计算一切:

SELECT 
    users.id,
    users.username,
    users.avatar_location,
    users.datetime,
    users.last_action,
    users.last_action_description,
    users.is_banned,
    COUNT(questions.questioner_id)
FROM 
    `users` 
LEFT JOIN 
    `questions` 
ON 
    users.id = questions.questioner_id
ORDER BY
    datetime 
    ASC 

我很抱歉,因为我的'noobiness'。我已经找了一两个小时关于如何做到这一点,但我无法解决它。

非常感谢

编辑:

非常感谢你的帮助!我的最后一个附加问题是,我现在还有一个第三个表格,这次我想计算现在我回答的问题。

表3(questions_answered): ID, question_id, 用户身份, 正确的(ETC ......)

我已经尝试将其添加到我的查询中,但不是显示已回答的问题数,而是重复问题的结果。

这是我更新的查询(对不起,我只是在努力解决这个问题)

SELECT 
    users.id,
    users.username,
    users.avatar_location,
    users.datetime,
    users.last_action,
    users.last_action_description,
    users.is_banned,
    COUNT(questions_answered.question_id),
    COUNT(questions.questioner_id)
FROM 
    `users` 
LEFT JOIN 
    `questions` 
ON 
users.id = questions.questioner_id
LEFT JOIN
    `questions_answered`
ON
    users.id = questions_answered.user_id
GROUP BY
    users.id,
    users.username,
    users.avatar_location,
    users.datetime,
    users.last_action,
    users.last_action_description,
    users.is_banned
ORDER BY
    datetime 
    ASC 

非常感谢你的帮助!

2 个答案:

答案 0 :(得分:1)

GROUP BY添加到您的查询中:

SELECT 
    users.id,
    users.username,
    users.avatar_location,
    users.datetime,
    users.last_action,
    users.last_action_description,
    users.is_banned,
    COUNT(questions.questioner_id)
FROM 
    `users` 
LEFT JOIN 
    `questions` 
ON 
    users.id = questions.questioner_id
GROUP BY
    users.id,
    users.username,
    users.avatar_location,
    users.datetime,
    users.last_action,
    users.last_action_description,
    users.is_banned
ORDER BY
    datetime 
    ASC 

答案 1 :(得分:1)

第一部分(两张表)

我在查询字符串中有我的评论:

SELECT 
    users.id,
    users.username,
    users.avatar_location,
    users.datetime,
    users.last_action,
    users.last_action_description,
    users.is_banned,
    COUNT(questions.id) as number_of_questions #count each question
FROM 
    `users` 
LEFT JOIN 
    `questions` 
ON 
    users.id = questions.questioner_id
GROUP BY
    users.id # you need to have unique user id in each row
ORDER BY
    questions.datetime # sort by question date right? 
    DESC 

更新(三张桌子):

您需要嵌套选择,不能同时对两列执行计数。我们把拳头选为users_questions_count像表一样,然后每个东西都和两个表一样。有缺陷的部分可能会对名称产生歧义。

SELECT 
    users_questions_count.id,
    users_questions_count.username,
    users_questions_count.avatar_location,
    users_questions_count.datetime,
    users_questions_count.last_action,
    users_questions_count.last_action_description,
    users_questions_count.is_banned,
    users_questions_count.number_of_questions,
    COUNT(questions_answered.id) as number_of_answers # make sure you are counting the correct field!
FROM 
    (SELECT 
        users.id as id,
        users.username as username,
        users.avatar_location as avatar_location,
        users.datetime as datetime,
        users.last_action as last_action,
        users.last_action_description as last_action_description,
        users.is_banned as is_banned,
        COUNT(questions.id) as number_of_questions #count each question
    FROM 
        `users` 
    LEFT JOIN 
        `questions` 
    ON 
        users.id = questions.questioner_id
    GROUP BY
        users.id 
    ) as users_questions_count
LEFT JOIN 
    `questions_answered` 
ON 
    users_questions_count.id = questions_answered.user_id
GROUP BY
    users_questions_count.id 

三个表格的一般想法(table1table2table3加入table1.idtable2.item_idtable3.item_id):

SELECT 
    table1_table2_count.id,
    table1_table2_count.counter_1,
    COUNT(table3.id) as counter_2
FROM 
    (SELECT 
        table1.id as id,
        COUNT(table2.id) as counter_1
    FROM 
        `table1` 
    LEFT JOIN 
        `table2` 
    ON 
        table1.id = table2.item_id
    GROUP BY
        table1.id 
    ) as table1_table2_count
LEFT JOIN 
    `table3` 
ON 
    table1_table2_count.id = table3.item_id
GROUP BY
    table1_table2_count.id 

但最好找到一种解决方案,以避免在几乎庞大的数据中进行这些选择。如果您可以更新一个表并在其上放置counter_1counter_2,则写入过程将变慢,但阅读(搜索)将会更快。