结合MySQL查询

时间:2011-11-18 01:04:26

标签: mysql join subquery

我正在努力找到合并这两个查询......我尝试过subquerys,join,union,并且没有运气:(

查询A

SELECT DATE(crtdtime) AS cntldate, sd_class, COUNT(crtdtime) AS created
FROM master
WHERE DATE(crtdtime) = '2011-11-16'
GROUP BY cntldate, sd_class

哪个产生

+------------+----------+---------+
| cntldate   | sd_class | created |
+------------+----------+---------+
| 2011-11-16 | CUST     |    2226 |
| 2011-11-16 | NET      |     238 |
+------------+----------+---------+

查询B

SELECT DATE(rstdtime) AS cntldate, sd_class, COUNT(rstdtime) AS restored
FROM master
WHERE DATE(rstdtime) = '2011-11-16'
GROUP BY cntldate, sd_class

哪个产生

+------------+----------+----------+
| cntldate   | sd_class | restored |
+------------+----------+----------+
| 2011-11-16 | CUST     |     2315 |
| 2011-11-16 | NET      |      221 |
+------------+----------+----------+

但希望最终结果是......

+------------+----------+---------+----------+
| cntldate   | sd_class | created | restored |
+------------+----------+---------+----------+
| 2011-11-16 | CUST     |    2226 |     2315 |
| 2011-11-16 | NET      |     238 |      221 |
+------------+----------+---------+----------+

非常感谢任何帮助。

谢谢。

5 个答案:

答案 0 :(得分:2)

一种可能的方式

SELECT DATE(crtdtime) AS cntldate, 
  sd_class, 
  sum( DATE(crtdtime) = '2011-11-16') AS created
  sum( DATE(rstdtime) = '2011-11-16') AS restored
FROM master
WHERE DATE(crtdtime) = '2011-11-16' or DATE(rstdtime) = '2011-11-16'
GROUP BY cntldate, sd_class

答案 1 :(得分:0)

使用

SELECT C.cntldate, C.sd_class, C.created, R.restored
FROM
(
SELECT DATE(crtdtime) AS cntldate, sd_class, COUNT(crtdtime) AS created
FROM master
GROUP BY cntldate, sd_class
) C
INNER JOIN
(
SELECT DATE(rstdtime) AS cntldate, sd_class, COUNT(rstdtime) AS restored
FROM master
GROUP BY cntldate, sd_class
) R
ON R.cntldate = C.cntldate AND R.sd_class = C.sd_class
WHERE C.cntldate = '2011-11-16' AND R.cntldate = '2011-11-16'

答案 2 :(得分:0)

怎么样

select q1.cntldate, q1.sd_class, q1.created, q2.restored 
   from  (
   SELECT DATE(crtdtime) AS cntldate, sd_class, COUNT(crtdtime) AS created
      FROM master
      WHERE DATE(crtdtime) = '2011-11-16'
      GROUP BY cntldate, sd_class
   )  q1
   inner join (
      SELECT DATE(rstdtime) AS cntldate, sd_class, COUNT(rstdtime) AS restored
         FROM master
         WHERE DATE(rstdtime) = '2011-11-16'
         GROUP BY cntldate, sd_class
  ) q2
      on q1.cntldate=q2.cntldate and q1.sdclass = q2.sdclass

这是Postgres的语法,所以可能必须使用它才能使它与MySql一起运行

答案 3 :(得分:0)

我相信这会奏效:

SELECT DATE(m1.crtdtime) AS cntldate, 
m1.sd_class, 
COUNT(m1.crtdtime) AS created , 
(
  SELECT COUNT(m2.rstdtime) 
  FROM master m2
  WHERE DATE(m2.rstdtime) = DATE(m1.rstdtime)
  and m2.sd_class=m1.sd_class
) as restored
FROM master m1
WHERE DATE(crtdtime) = '2011-11-16'
GROUP BY cntldate, sd_class

答案 4 :(得分:0)

实际上并不困难(一旦你做了一段时间)。使用SUM(IF()),例如......

SELECT 
      date( if( date( ctrdtime ) = '2011-11-16', ctrdtime, rstdtime )) as CntlDate,
      sd_class,
      sum( if( date( ctrdtime ) = '2011-11-16', 1, 0 ) ) as CreatedCount,
      sum( if( date( rstdtime ) = '2011-11-16', 1, 0 ) ) as RestoredCount
   FROM 
      master
   WHERE 
         DATE(crtdtime) = '2011-11-16'
      OR DATE(rstdtime) = '2011-11-16'
   GROUP BY 
      1, 2

如果您为已创建和已恢复的日期范围扩展了......