通过使用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语句。我该怎么写呢?
答案 0 :(得分: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
将为NULL
列返回rs_repuations.value
,然后由0
替换为COALESCE
。
如果您不熟悉JOIN,那么有一个很棒的visual guide by 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 - 联盟
您有几个选择 - 您可以像这样UNION结果:
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 - 加入相同的表格两次
或者您可以通过using the same table name but a different alias两次自行加入同一张桌子:
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 |
最后(phew)原始SQL中出现错误的原因是您将两个SELECT语句链接在一起而没有实际关联它们 - 在这个实例中,SQL实际上没有意义,因为您需要在逻辑上将它们关联起来(或者通过UNION或者按照上面的重复连接。