1

通过使用 COALESCE,我可以创建一个名为 comment_votes 的临时变量,如下所示:

SELECT comments.*, COALESCE(rs_reputations.value, 0) AS comment_votes FROM `comments` 
LEFT JOIN rs_reputations ON comments.id = rs_reputations.target_id AND 
rs_reputations.target_type = 'Comment' AND rs_reputations.reputation_name = 
'comment_votes' AND rs_reputations.active = 1 WHERE (impression_id = 1)

我想在来的查询中创建第二个变量,称为impression_votes。我试图这样做:

SELECT comments.*, COALESCE(rs_reputations.value, 0) AS comment_votes 
FROM 'comments' 
    LEFT JOIN rs_reputations ON 
        comments.id = rs_reputations.target_id AND 
        rs_reputations.target_type = 'Comment' AND 
        rs_reputations.reputation_name = 'comment_votes' AND 
        rs_reputations.active = 1 
SELECT comments.*, COALESCE(rs_reputations.value, 0) AS impression_votes 
FROM 'comments' 
    LEFT JOIN rs_reputations ON 
    comments.id = rs_reputations.target_id AND
    rs_reputations.target_type = 'Comment' AND
    rs_reputations.reputation_name = 'impression_votes' AND 
    rs_reputations.active = 1 
WHERE

这会导致错误:

You have an error in your SQL syntax

我正在尝试的甚至可能吗?如果是这样,我似乎不正确地桥接了两个 SELECT/COALESCE 语句。我该怎么写这个?

4

1 回答 1

2

MySQL COALESCE函数实际上是一个内置函数,它返回第一个非空值——它不是一个变量,它是一个实际上被各种数据库系统支持的函数。

例如,使用下表:

| Id    | Name      | Counter    |
| 1     | lolcat    | NULL       |
| 2     | codez     | 1          |

sql语句:

SELECT Id, Name, COALESCE(counter, 0) AS NonNullCounter FROM table

将返回结果:

| Id    | Name      | NonNullCounter |
| 1     | lolcat    | 0              |
| 2     | codez     | 1              |

在这种情况下,NULL 值已被 0 替换。

这对您很有用,因为如果您在评论中的行的 rs_reputations 中还没有任何匹配的行,LEFT JOIN则将返回NULLcolumn rs_repuations.value,然后将其替换为0by COALESCE

如果您是 JOIN 新手,那么Jeff Atwood 提供了一个很棒的视觉指南

您的第一个查询实际上是:

SELECT     comments.*, 
           COALESCE(rs_reputations.value, 0) AS comment_votes 
FROM       comments 
LEFT JOIN   rs_reputations ON comments.id = rs_reputations.target_id 
                           AND rs_reputations.reputation_name = 'comment_votes' 
WHERE       impression_id = 1;

选择 1 - 工会

您有几个选择 - 您可以像这样将您的结果联合起来

SELECT     comments.*, 
           COALESCE(rs_reputations.value, 0) AS votes,
           'comment_votes' AS vote_type 
FROM       comments 
LEFT JOIN   rs_reputations ON comments.id = rs_reputations.target_id 
                           AND rs_reputations.reputation_name = 'comment_votes' 
WHERE       impression_id = 1

UNION

SELECT     comments.*, 
           COALESCE(rs_reputations.value, 0) AS votes,
           'impression_votes' as vote_type 
FROM       comments 
LEFT JOIN   rs_reputations ON comments.id = rs_reputations.target_id 
                           AND rs_reputations.reputation_name = 'impression_votes' 
WHERE       impression_id = 1;

在这种情况下,您的结果将如下所示:

|comments_columns|votes|vote_type       |
| *              |12   |comment_vote    |
| *              |2    |impression_vote |  

选择 2 - 加入同一张桌子两次

或者,您可以通过使用相同的表名但使用不同的别名来两次自我加入同一个表:

SELECT     comments.*, 
           COALESCE(CommentRep.value, 0) AS comment_votes,
           COALESCE(ImpressionRep.value, 0) AS impression_votes,
FROM       comments 
LEFT JOIN   rs_reputations AS CommentRep ON comments.id = CommentRep.target_id 
                           AND CommentRep.reputation_name = 'comment_votes' 
LEFT JOIN   rs_reputations AS ImpressionRep ON comments.id = ImpressionRep.target_id 
                           AND ImpressionRep.reputation_name = 'impression_votes'
WHERE       CommentRep.impression_id = 1
AND         ImpressionRep.impression_id = 1

在这种情况下,您的结果将如下所示:

|comments_columns|comment_votes|impression_votes|
| *              |12           |0               |
| *              |2            |6               |  

最后(呸)您在原始 SQL 中出现错误的原因是您将两个 SELECT 语句链接在一起而没有实际关联它们 - 在这种情况下 SQL 并没有真正意义,因为您需要在逻辑上关联它们(或者通过UNION 或按照上面的重复连接。

于 2012-10-17T22:57:08.287 回答