Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8705813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:27:32+00:00 2026-06-13T03:27:32+00:00

By using COALESCE, I can create a temporary variable called comment_votes like so: SELECT

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:27:33+00:00Added an answer on June 13, 2026 at 3:27 am

    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:

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

    The sql statement:

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

    will return the results:

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

    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 JOIN will return NULL for the column rs_repuations.value, which is then replaced by 0 by COALESCE.

    If you are new to JOINs then there is a great visual guide by Jeff Atwood.

    Your first query can is actually:

    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;
    

    CHOICE 1 – UNION

    You have a couple of choices – you can either UNION your results together like this:

    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;
    

    In this instance your results will look like this:

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

    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:

    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
    

    In this instance your results will look like this:

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

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From this question , a neat answer about using COALESCE to simplify complex logic
C++Builder ADOQuery SQLServer I'm using a stored procedure with this select SELECT Name, COALESCE(
We are using a query that uses coalesce to compare potentially null values. I
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question
Using SQL Server 2008 R2 we are looking for a way to select the
Using Location.getBearing(); I seem to get randomly changing bearings. Aka, I can turn the
i try to use sum and Coalesce . How can i translate to linq?
I'm using a generic system for reporting which takes data from a database view
function toltalRetailerComm($userId) { //print_r ($shoppeId); $sql = SELECT shoppe_id FROM atm_super_shoppe WHERE user_id='$userId'; $query
I have a stored procedure as follows: CREATE PROC [dbo].[Incidents] (@SiteName varchar(200)) AS SELECT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.