By using COALESCE, I can create a temporary variable called comment_votes like so:
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)
I want to create a second variable called impression_votes in the came query. I attempted to do this with:
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
This leads to the error:
You have an error in your SQL syntax
Is what I’m attempting even possible? If so, I seem to be bridging the two SELECT/COALESCE statements improperly. How should I write this?
The MySQL COALESCE function is actually an inbuilt function that returns the first non-null value – it’s not a variable, it’s a function that is actually supported across a wide variety of database systems.
For example, with the following table:
The sql statement:
will return the results:
In this instance, the NULL value has been replaced by 0.
This is useful for you as, if you don’t yet have any matching rows in rs_reputations for the row in comments, the
LEFT JOINwill returnNULLfor the columnrs_repuations.value, which is then replaced by0byCOALESCE.If you are new to JOINs then there is a great visual guide by Jeff Atwood.
Your first query can is actually:
CHOICE 1 – UNION
You have a couple of choices – you can either UNION your results together like this:
In this instance your results will look like this:
CHOICE 2 – JOIN ON TO THE SAME TABLE TWICE
Or you can self join onto the same table twice by using the same table name but a different alias:
In this instance your results will look like this:
Finally (phew) the reason you have an error in your original SQL is that you are chaining two SELECT statements together without actually relating them – the SQL doesn’t really make sense in this instance as you need to logically relate them (either via a UNION or a repeated join as per above.