mySQL查询多个组

时间:2012-07-12 15:26:29

标签: mysql group-by

如何将这两个查询合并为1并在大型表上高效运行?

SELECT  field1, count(1) as requestCount
FROM table1
WHERE date_complete >= '2012-06-12 00:00:00' 
      AND date_complete <= '2012-07-12 23:59:59' 
GROUP BY field1

SELECT field2, count(1) as completeCount
FROM table1
WHERE date_complete >= '2012-06-12 00:00:00' 
      AND date_complete <= '2012-07-12 23:59:59' 
GROUP BY field2

表包含涉及多个用户的流程的信息。例如,假设第一个人创建请求,第二个人完成请求,第三个人通过提交请求来关闭请求。

我想计算每个用户在特定时间范围内请求,填写和归档的人数

我希望这两个组合看起来像

+----------------+--------------+
| field1         | requestCount |
+----------------+--------------+
| PJB            |            1 |
| RFD            |            6 |
| YAS            |            4 |
+----------------+--------------+

+

+---------+---------------+
| field2  | completeCount |
+---------+---------------+
| PJB     |            4  |
| YAS     |            5  |
+---------+---------------+

=

+----------------+--------------+---------------+
| Username       | requestCount | completeCount |
+----------------+--------------+---------------+
| PJB            |            1 |             4 |
| RFD            |            6 |             0 |
| YAS            |            4 |             5 |
+----------------+--------------+---------------+

4 个答案:

答案 0 :(得分:1)

在这里猜测,但我认为你需要:

SELECT
    u .Username
  , t1.requestCount
  , t2.completeCount
FROM
        ( SELECT field1 AS Username
          FROM table1
          UNION
          SELECT field2
          FROM table2
        ) AS u
    LEFT JOIN
        ( SELECT field1
               , COUNT(*) AS requestCount
          FROM table1
          WHERE date_complete >= '2012-06-12' 
            AND date_complete < '2012-07-13' 
          GROUP BY field1
        ) AS t1  
            ON t1.field1 = u.Username
    LEFT JOIN
        ( SELECT field2
               , COUNT(*) AS completeCount
          FROM table1
          WHERE date_complete >= '2012-06-12' 
            AND date_complete < '2012-07-13' 
          GROUP BY field2 
        ) AS t2
            ON t2.field2 = u.Username   
;

如果您有user表,只需将第一个派生表替换为user AS u

答案 1 :(得分:1)

select  o.USERNAME, a.requestCount , b.completeCount from (SELECT  USERNAME
FROM owner_login_pass )
 as o left join (SELECT field1, count(1) as requestCount
FROM table1
WHERE date_complete between '2012-06-12 00:00:00' and '2012-07-12 23:59:59' 
GROUP BY field1) as a on o.USERNAME=a.field1
left join (SELECT field2, count(1) as completeCount
FROM table1
WHERE date_complete between '2012-06-12 00:00:00' and '2012-07-12 23:59:59' 
GROUP BY field2) as b on o.USERNAME=b.field2

您需要确保field1具有显示数据所需的所有用户。否则,您还需要加入用户表。

答案 2 :(得分:0)

我认为您不能将它们合并(UNION除外),但您可以使用以下方法稍加改进:WHERE date_complete BETWEEN '2012-06-12 00:00:00' AND '2012-07-12 23:59:59'

答案 3 :(得分:0)

你需要:::

Select * from 
(
   SELECT  field1, count(1) as requestCount
FROM table1
WHERE date_complete between '2012-06-12 00:00:00' and '2012-07-12 23:59:59'  
GROUP BY field1 ) tab1
left join
(
SELECT fied2, count(1) as completeCount
FROM table1
WHERE date_complete between '2012-06-12 00:00:00' and '2012-07-12 23:59:59' 
GROUP BY field2) tab2 on (tab1.field1 = tab2.field2)