I know it’s not a good idea to loop SQL queries, so I would like to change these two queries into one.
Not being too great at JOINs, I’m struggling to figure how to merge these two, with it having that DISTINCT in there.
$sql_result2 = mysql_query("SELECT DISTINCT gang FROM turfs WHERE city='$city'", $db);
while ($rs2 = mysql_fetch_array($sql_result2)) {
$sql_result3 = mysql_query("SELECT (SELECT COUNT(*) FROM turfs WHERE city='$city' AND gang=g.id) as count, g.id, g.name, g.color FROM gangs g WHERE g.id='$rs2[gang]'", $db);
$rs3 = mysql_fetch_array($sql_result3);
Any suggestions?
Join and group with something like: