SQL Multiple COUNT() from two tables, within a LEFT JOIN -



SQL Multiple COUNT() from two tables, within a LEFT JOIN -

i have 3 tables, want select info 1 of tables, , collect count of how many times specific row of info linked in other 2 tables.

so, select info site_projects. homecoming count of site_project_members site_projects.id = site_project_members.pid , homecoming count of site_project_tasks site_projects.id = site_project_members.pid

i hope i'm making sense, query, looks correct. , querys database (mysql) no problems. except returns sum of both of counts both things. (see below table structures)

site_projects

id | title | desc | start | deadline | progress 1 | project 1 | project | 1321748906 | 1329847200 | 20

site_project_members

id | pid | uid | img | hidden 1 | 1 | 1 | 1 | 0 2 | 1 | 2 | 2 | 0

site_project_tasks

id | pid | desc | completed 1 | 1 | task 1 | 1 1 | 1 | task 2 | 0

here query:

select p.`id`, p.`title`, p.`desc`, p.`progress`, p.`start`, p.`deadline`, count(m.`id`) `members`, count(t.`id`) `tasks` `site_projects` p left bring together `site_project_members` m on p.`id`=m.`pid` left outer bring together `site_project_tasks` t on p.`id`=t.`pid` order p.`id` asc

the result is:

id | title | desc | progress | start | deadline | members | tasks 1 | project 1 | project | 20 | 1321748906 | 1329847200 | 4 | 4

both of "4" values should 2. not :s able help? much appreciated

thanks, dan

select p.id , p.title , p.desc , p.progress , p.start , p.deadline , coalesce( m.cnt, 0 ) members, coalesce( t.cnt, 0 ) tasks site_projects p left bring together ( select pid, count(*) cnt site_project_members grouping pid ) m on p.id = m.pid left bring together ( select pid, count(*) cnt site_project_tasks grouping pid ) t on p.id = t.pid order p.id asc

sql join count

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -