I am using this query to return return a list of songs stored in $sTable along with a COUNT of their total projects which are stored in $sTable2.
/*
* SQL queries
* Get data to display
*/
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable b
LEFT JOIN (
SELECT COUNT(*) AS projects_count, a.songs_id
FROM $sTable2 a
GROUP BY a.songs_id
) bb ON bb.songs_id = b.songsID
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
'projects_count' is put into an array along with the columns in '$sTable', this is then spat out via JSON and displayed in a table on page.
This is working perfectly apart from when a song has no projects linked to it. It of course returns NULL.
All I want is for any null values to be returned as a '0'.
I have tried the COUNT(), COUNT(IFNULL (project_id,0) and using COUNT(DISTINCT)...
And also:-
SELECT COALESCE(COUNT(*),0) AS projects_count, a.songs_id
All without success.
Any ideas?