2009-09-16 74 views
1

試圖在一個查詢中獲取兩個不同的計數。要做到這一點,我從表中選擇,需要加入另一個。查看下面的(不工作)查詢。這是我嘗試去做的不同方式 - 不成功。在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 

回答

4
SELECT 
projects.* 
, (SELECT count(*) FROM tasks WHERE project_id = projects.project_id AND assigned_user_id = 1) as task_assigned_count, 
, (SELECT count(*) FROM tasks WHERE project_id = projects.project_id AND created_user_id = 1) as task_created_count 
FROM projects 
-1
SELECT (SELECT COUNT(task_id) FROM tasks_assigned) AS task_assigned_count, (SELECT COUNT(task_id) FROM tasks_created) AS task_created_count 
0

在子查詢擺脫分組:

SELECT 
     projects.*, 
    (SELECT count(task_id) FROM tasks 
     WHERE task_assigned.project_id = projects.project_id 
     AND assigned_user_id = 1) as task_assigned_count, 
... 
FROM projects 
0

不是100%,如果這是你正在嘗試做的,但它看起來像你應該分組按項目ID。你可以這樣做:

SELECT projects.project_id, assigned_count.count, created_count.count 
FROM projects 
LEFT JOIN (SELECT project_id, 
        count(*) AS count, 
      FROM projects 
      INNER JOIN tasks A 
        ON tasks.project_id = projects.project_id 
      WHERE tasks.assigned_user_id = 1 
      GROUP BY projects.project_id) assigned_count 
ON assigned_count.project_id = projects.project_id 
LEFT JOIN (SELECT project_id, 
        count(*) AS count, 
      FROM projects 
      INNER JOIN tasks A 
        ON tasks.project_id = projects.project_id 
      WHERE tasks.created_user_id = 1 
      GROUP BY projects.project_id) created_count 
ON created_count.project_id = projects.project_id 

或:

SELECT projects.*, 
     SUM(IF(tasks.assigned_user_id = 1,  1,0)) AS task_assigned_count, 
     SUM(IF(tasks_created.created_user_id = 1,1,0)) AS task_created_count 
FROM projects 
LEFT JOIN tasks A 
     ON tasks.project_id = projects.project_id 
GROUP BY projects.project_id 
0

我會在兩個查詢做到這一點:

SELECT p.project_id, COUNT(a.task_id) AS task_assigned_count 
FROM projects p 
LEFT JOIN tasks AS a ON a.project_id = p.project_id 
    AND a.assigned_user_id = 1 
GROUP BY p.project_id; 

SELECT p.project_id, COUNT(c.task_id) AS task_created_count 
FROM projects p 
LEFT JOIN tasks AS c ON c.project_id = p.project_id 
    AND a.created_user_id = 1 
GROUP BY p.project_id; 

否則,你所創建的tasks的兩個實例之間的笛卡爾乘積。您也可以使用DISTINCT,但這只是補償笛卡爾產品,而不是避免:

SELECT p.project_id, COUNT(DISTINCT a.task_id) AS task_assigned_count, 
    COUNT(DISTINCT c.task_id) AS task_created_count 
FROM projects p 
LEFT JOIN tasks AS a ON a.project_id = p.project_id 
    AND a.assigned_user_id = 1 
LEFT JOIN tasks AS c ON c.project_id = p.project_id 
    AND a.created_user_id = 1 
GROUP BY p.project_id; 
相關問題