在1个查询中选择2个计数

时间:2009-09-16 20:20:02

标签: mysql

尝试在一个查询中获得两个不同的计数。要做到这一点,我从表中选择,需要加入另一个。查看下面的(不工作)查询。这些是我试图做的不同方式 - 不成功。

这可能,如果是这样,你可以提供协助获得有效的查询吗?

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

5 个答案:

答案 0 :(得分: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 :(得分: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

答案 2 :(得分: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

答案 3 :(得分: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;

答案 4 :(得分:-1)

SELECT (SELECT COUNT(task_id) FROM tasks_assigned) AS task_assigned_count, (SELECT COUNT(task_id) FROM tasks_created) AS task_created_count