-
Order by count
I have a query that orders results by the count of another table:
"SELECT table1.*, COUNT(table2.id) AS rev_count FROM table1
LEFT JOIN table2 ON table1.name = table2.name
WHERE table1.active = 1 AND table1.city='$city'
GROUP BY table1.name
ORDER BY rev_count DESC";
How do you add a condition (ie WHERE active=1) to just the "COUNT(table2.id) AS rev_count" part of the query?
-
Code:
SELECT table1.*
, COUNT(table2.id) AS rev_count
FROM table1
LEFT
JOIN table2
ON table2.name = table1.name
AND table2.active = 1
WHERE table1.active = 1
AND table1.city = '$city'
GROUP
BY table1.name
ORDER
BY rev_count DESC
-
Ah yes of course! Cheers!