现在有如下3张表
a(总量)
-----------------------
1   2008-08-12 10:42:09
2   2008-11-27 10:16:09
3   2008-11-27 12:42:09
4   2008-11-27 10:44:09
5   2008-12-05 10:42:09
-----------------------b(成功)
-----------------------
1   2008-11-27 10:16:09
2   2008-11-27 10:42:09
3   2008-08-12 10:42:09
-----------------------c(失败)
-----------------------
1   2008-11-27 10:44:09
2   2008-12-05 10:42:09
-----------------------现在我想用一条SQL语句就查询出11月份中每天 a的总量,b的总量,c的总量,该怎么查询啊?
分开了我会做如下:
select date_format(create_date,"%Y-%m-%d") as time,count(*) as total
from t_cr_ringlib_diy where date_format(create_date,"%Y-%m")='2008-11' group by day(create_date)

解决方案 »

  1.   

    select * from (
    select date_format(create_date,"%Y-%m") as aa,count(*) from a 
    where date_format(create_date,"%Y-%m")='2008-11
    group by  date_format(create_date,"%Y-%m")) a1left join
    (
    select date_format(create_date,"%Y-%m") as bb,count(*) from b 
    where date_format(create_date,"%Y-%m")='2008-11
    group by  date_format(create_date,"%Y-%m")) a2
    on a1.aa=a2.bbleft join
    (
    select date_format(create_date,"%Y-%m") as cc,count(*) from c
    where date_format(create_date,"%Y-%m")='2008-11
    group by  date_format(create_date,"%Y-%m")) a3
    on a1.aa=a3.cc
      

  2.   

    恩,解决了,就是不是很理解为什么这样写!
    select * from (
    select date_format(create_date,"%Y-%m-%d") as time,count(*) as total from t_cr_ringlib_diy
    where date_format(create_date,"%Y-%m")='2008-11'
    group by  day(create_date)) t 比如第一句,select * from()t 这样写什么意思不太明白
    还请WWWWA指教一下啊!
      

  3.   

    MYSQL的语法规定在嵌套查询中要加入别名
      

  4.   

    select date_format(create_date,"%Y-%m-%d") as time,count(*) as total from t_cr_ringlib_diy
    where date_format(create_date,"%Y-%m")='2008-11'
    group by  day(create_date)以上是一个查询结果啊,为什么放在from的后面呢? from的后面不是要放查询表的吗?
      

  5.   

    就单从
    select * from (
    select date_format(create_date,"%Y-%m-%d") as time,count(*) as total from t_cr_ringlib_diy
    where date_format(create_date,"%Y-%m")='2008-11'
    group by  day(create_date)) t 
    这句来讲,不用SELECT * FROM () t,但你的查询是三张表分组再连接,
    就要加SELECT * FROM ()
      

  6.   

    但你的查询是三张表分组再连接, 不能直接连接(否则结果有误),就要加SELECT * FROM ()
      

  7.   

    恩,有点明白了,在琢磨琢磨,估计今天能弄懂了!
    非常感谢WWWWA!