cant order NULL value properly (mysql)

Posted on

Question :

So I got this view in mysql :

CREATE VIEW vote_question_totals
SELECT question_id
, SUM((vote_question_type)='1') question_total_plus
, SUM((vote_question_type)='-1') question_total_minus
, count(*) question_total_mixed
,(SUM((vote_question_type)='1') - SUM((vote_question_type)='-1')) question_total_score
FROM vote_question
GROUP BY question_id;

and I’m doing a RIGHT JOIN with the question table using the question_id

but then I wanna ORDER by question_total_score and this is where it get problematical.
The question with no vote get a question_total_score which is NULL and cant get properly ordered.
(I would like my NULL row to be considered like a 0)

What should I do ?

Answer :

    COALESCE(question_total_score,0) AS `score`, 
   `vote_question_totals` RIGHT JOIN `question` 
        ON vote_question_totals.question_id = question.question_id 
   `score` DESC

Leave a Reply

Your email address will not be published. Required fields are marked *