如果没有结果,MySql选择默认值?

时间:2010-05-27 15:49:27

标签: mysql

我有两张桌子:成员和评论。 我选择所有成员,然后加入评论。 但是在评论中我选择了一些点数的SUM,如果用户从未评论过,我无法在列表中获得该用户?!

如果用户从未注释过,或者其他一些解决方案如何选择SUM的默认值为0:

SELECT c.comment_id AS item_id, m.member_id AS member_id, m.avatar, 
            SUM(c.vote_value) AS vote_value, SUM(c.best) AS best, 
            SUM(c.vote_value) + SUM(c.best)*10 AS total
            FROM members m
            LEFT JOIN comments c ON m.member_id = c.author_id
            GROUP BY c.author_id
            ORDER BY m.member_id DESC
            LIMIT 0, 20

编辑:

我会试着解释一下...... 所以有2个表,成员和评论。我需要列出所有排名的用户。 评论持有所有选票和最佳答案。

所以,我需要列出所有用户,然后他们得分。

会员表:

member_id - username - avatar

评论表

comment_id - author_id - vote_value - best (0 OR 1)

还尝试从COMMENTS中选择并加入MEMBERS,但同样的事情:(

6 个答案:

答案 0 :(得分:21)

如果您只是想要用户及其排名,我不确定您为什么要将SELECT_id包含在SELECT列表中。你只想要他们对那个特定评论的排名吗?我现在给出一个解决方案,假设您只想要一个包含排名的完整成员列表:

SELECT
    M.member_id,
    M.user_id,
    M.avatar,
    COALESCE(SUM(C.vote_value), 0) AS vote_value_sum,
    COALESCE(SUM(C.best), 0) AS best_sum,
    COALESCE(SUM(C.vote_value), 0) + SUM(C.best) * 10 AS total_value
FROM
    Members M
LEFT OUTER JOIN Comments C ON
    C.author_id = M.member_id
GROUP BY
    M.member_id
ORDER BY
    total_value DESC
LIMIT 0, 20

(这假设vote_value和best是非NULL列或者MySQL在计算SUM值时会忽略它们 - 我相信它确实如此,但我没有测试过它)

答案 1 :(得分:20)

MySQL有一个IFNULL运算符,如果结果为空,它允许你返回null以外的值。

SELECT c.comment_id AS item_id, m.member_id AS member_id, m.avatar, 
            SUM(IFNULL(c.vote_value, 0)) AS vote_value, SUM(IFNULL(c.best, 0)) AS best, 
            SUM(IFNULL(c.vote_value, 0)) + SUM(IFNULL(c.best, 0))*10 AS total
            FROM members m
            LEFT JOIN comments c ON m.member_id = c.author_id
            GROUP BY c.author_id
            ORDER BY m.member_id DESC
            LIMIT 0, 20

正如其他人所提到的,COALESCE做了类似的事情(也适用于MySQL)。

答案 2 :(得分:6)

我使用COALESCE来做这类事情。

http://dev.mysql.com/doc/refman/5.1/en/comparison-operators.html#function_coalesce

我会将你的第二行和第三行重写为:

COALESCE(SUM(c.vote_value), 0) AS vote_value, COALESCE(SUM(c.best),0) AS best,
COALESCE(SUM(c.vote_value), 0) + COALESCE(SUM(c.best),0) * 10 AS total

答案 3 :(得分:2)

我认为SUM不是问题所在。我想这可能是你的

GROUP BY c.author_id

您在左外连接的右侧进行分组。我不确定当右侧不存在时会发生什么,但我猜这不是你真正想要的。你很可能应该按成员分组,而不是评论。

话虽如此,是的,您还应该使用COALESCE或IFNULL将空值转换为0。

答案 4 :(得分:-1)

您可以使用if语句将NULL转换为0

SELECT c.comment_id AS item_id, ...
    IF(SUM(c.vote_value) is null, 0, SUM(c.vote_value)) as vote_value
FROM members m
LEFT JOIN comments c ON ...

答案 5 :(得分:-1)

您也可以查看CASE声明:http://dev.mysql.com/doc/refman/5.0/en/case-statement.html

在您的情况下,结果类似于......

SELECT c.comment_id AS item_id, m.member_id AS member_id, m.avatar, 
        (CASE SUM(c.vote_value) WHEN NULL THEN 0  
        ELSE SUM(c.vote_value) END) AS vote_value, SUM(c.best) AS best, 
        (CASE SUM(c.vote_value) + SUM(c.najbolji)*10 WHEN null THEN 0
        ELSE SUM(c.vote_value) + SUM(c.najbolji)*10 END) AS total
        FROM members m
        LEFT JOIN comments c ON m.member_id = c.author_id
        GROUP BY c.author_id
        ORDER BY m.member_id DESC
        LIMIT 0, 20