Mysql double left outer join with count

Anorionil

I have the following DB structure:

  • Song (ID, ...)
  • Review (SongID, Accept)

I want to view all songs and count how many accepts and how many declines a song has.

I have the following query:

SELECT s.*, COUNT(ra.ID) as Accepts, COUNT(rd.ID) as Declines 
FROM song s
LEFT OUTER JOIN review ra ON s.ID = ra.SongID AND ra.Accept = 1
LEFT OUTER JOIN review rd ON s.ID = rd.SongID AND rd.Accept = 0
GROUP BY s.ID

This doesn't yield the correct result. I have a song with 3 accepts and 1 decline, the query outputs 3 accepts and 3 declines. How do I fix this?

Mosty Mostacho

The issue with juergen's solution is that if a song has no review then r.accept will be null. When you compare a null value using = you get null as a result. (You should use IS in order to compare null values). Then, if you SUM (add) null values then you get a null value again.

So, you should make sure that when a song has no review, it will return 0 instead of null:

SELECT s.ID, 
       COALESCE(SUM(r.Accept = 1), 0) as Accepts, 
       COALESCE(SUM(r.Accept = 0), 0) as Declines 
FROM song s
LEFT OUTER JOIN review r ON s.ID = r.SongID
GROUP BY s.ID

COALESCE will take the first non-null argument from the list, so if the SUM is null then a 0 will be put in its place.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MYSQL Count, LEFT OUTER JOIN with 0 count on a simple case

From Dev

Linq Left Outer Join with Count

From Dev

MySQL Left Outer Join not returning NULL values for COUNT(*)

From Dev

MySQL "Left Outer Join" Issue

From Dev

Left outer join on Slick/Mysql

From Dev

MySql left outer join with condiction

From Dev

Mysql Left outer join filter by

From Dev

MySQL "Left Outer Join" Issue

From Dev

Mysql INNER JOIN after a LEFT OUTER JOIN

From Dev

SQL incremential count for X with a Left Outer Join

From Dev

SQL COUNT() function and LEFT OUTER JOIN

From Dev

MySQL LEFT OUTER JOIN not filtering records

From Dev

mysql - how to UPDATE after LEFT OUTER JOIN

From Dev

mysql left outer join with two conditions

From Dev

MySQL LEFT OUTER JOIN not filtering records

From Dev

Select rows with Left Outer Join and condition - MySQL

From Dev

MySql Left outer Join not working as expected

From Dev

Mysql Left OUTER JOIN with Subquery (wordpress)

From Dev

MySQL - endless running query on LEFT OUTER JOIN

From Dev

MySQL LEFT OUTER JOIN not giving correct results

From Dev

MySQL left join count with condition

From Dev

MYSQL LEFT JOIN and COUNT and GROUP BY

From Dev

Left Join + count + sum on MySQL

From Dev

mysql Left Join, group by with count

From Dev

MySQL query with COUNT ON LEFT JOIN

From Dev

Outer join with a subquery changes the count of rows in mysql

From Dev

How to do a double left outer join in Linq query syntax(or fluent)

From Dev

How to show 0 count for absent facility on a left outer join

From Dev

LEFT OUTER JOIN with LIMIT