試圖在一個查詢中獲取兩個不同的計數。要做到這一點,我從表中選擇,需要加入另一個。查看下面的(不工作)查詢。這是我嘗試去做的不同方式 - 不成功。在1個查詢中選擇2個計數
這是可能的,如果是的話,你可以提供幫助獲得工作查詢嗎?
SELECT
count(tasks_assigned.task_id) AS task_assigned_count,
count(tasks_created.task_id) AS task_created_count,
FROM projects
LEFT JOIN tasks AS tasks_assigned
ON tasks_assigned.project_id = projects.project_id
AND tasks_assigned.assigned_user_id = 1
LEFT JOIN tasks AS tasks_created
ON tasks_created.project_id = projects.project_id
AND tasks_created.created_user_id = 1
GROUP BY tasks_assigned.project_id, tasks_created.project_id
SELECT
projects.*
, (SELECT count(task_id) as task_assigned_count FROM tasks as task_assigned WHERE task_assigned.project_id = projects.project_id AND assigned_user_id = 1 GROUP BY task_id) as task_assigned_count
, (SELECT count(task_id) as task_created_count FROM tasks as task_created WHERE task_created.project_id = projects.project_id AND assigned_user_id = 1 GROUP BY task_id) as task_assigned_count
FROM projects