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
Simply add this line in your code after SELECT
IF(projects_count IS NULL, 0, projects_count) As projects_countList
Like This:
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns)).",
IF(projects_countIS NULL, 0, projects_count) As projects_countList
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());
To return 0 instead of null in MySQL
USE
SELECT id, IF(age IS NULL, 0, age) FROM tblUser
USE with count() having join 2 tables
like
SELECT
tblA.tblA_Id,
tblA.Name,
tblC.SongCount,
IF(tblC.SongCount IS NULL, 0, tblC.SongCount) As noOfSong
FROM tblA
LEFT JOIN
(
SELECT
ArtistId,count(*) AS SongCount
FROM
tblB
GROUP BY
ArtistId
) AS tblC
ON
tblA.tblA_Id = NoOfSong.ArtistId
And Result is
tblA_Id Name SongCount noOfSong
-----------------------------------------------------
7 HSP NULL 0
6 MANI NULL 0
5 MEET 1 1
4 user NULL 0
3 jaani 2 2
2 ammy NULL 0
1 neha 2 2