[nycphp-talk] MySQL: count()children on 2 related tables in 1 query
Dan Cech
dcech at phpwerx.net
Tue Jan 4 16:02:05 EST 2011
Sorry, forgot the ON clauses:
SELECT parent.id, parent.someColumn, c1.cnt, c2.cnt
FROM parent
LEFT JOIN (
SELECT child_table_1.parent_id,count(child_table_1.id) as cnt
FROM child_table_1
GROUP BY child_table_1.parent_id
) AS c1 ON c1.parent_id=parent.id
LEFT JOIN (
SELECT child_table_2.parent_id,count(child_table_2.id) as cnt
FROM child_table_2
GROUP BY child_table_2.parent_id
) AS c2 ON c2.parent_id=parent.id
Dan
More information about the talk
mailing list