Mysql组的帮助

时间:2010-10-25 11:16:37

标签: mysql

几次尝试后,我无法解决这个问题。 我有以下mysql查询:

SELECT
                  date_format(connect_time,"%Y-%m-%d %H") AS date,
                  NotConnected,
                  count(id) as calls,
                  sum(`duration`)/60 as minutes
   FROM test.`CDR_Vendors`,

   (SELECT        
       date_format(connect_time,"%Y-%m-%d %H") AS date,
       Count(id) as NotConnected
       FROM `CDR_Vendors_Failed`
       inner join Vendors on (CDR_Vendors_Failed.i_vendor = Vendors.i_vendor)
       inner join Customers on (CDR_Vendors_Failed.i_customer = Customers.i_customer)
       WHERE
       Customers.name like "W%"
       and
       Vendors.name like "D%"
       and connect_time between curdate() and now()
       GROUP by date
       ORDER BY date

   )Failed
       inner join Vendors on (CDR_Vendors.i_vendor = Vendors.i_vendor)
       inner join Customers on (CDR_Vendors.i_customer = Customers.i_customer)
       WHERE
       Customers.name like "W%"
       and
       Vendors.name like "D%"
       and connect_time between curdate() and now()
       GROUP by date
       ORDER BY date

结果:

date,Notconnected,calls, minutes
'2010-10-25 00', 408, 900, 6611.00
'2010-10-25 01', 408, 456, 2777.60
'2010-10-25 02', 408, 204, 1545.80
'2010-10-25 03', 408, 108, 1951.80
'2010-10-25 04', 408, 192, 895.60
'2010-10-25 05', 408, 300, 544.20
'2010-10-25 06', 408, 540, 961.20
'2010-10-25 07', 408, 1728, 5027.60
'2010-10-25 08', 408, 4968, 20986.40
'2010-10-25 09', 408, 7884, 33065.00
'2010-10-25 10', 408, 7836, 28182.20
'2010-10-25 11', 408, 1800, 3587.80

我注意到重复了Notconnected字段的第一个值。

我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

此查询将非常慢。你在哪个表中有connect_time字段?如果在CDR_Vendoes和CRD_Vendors_Failed中,那么您不需要子查询。

也许您需要加入CDR_Vendors_Failed表?

SELECT
                  date_format(connect_time,"%Y-%m-%d %H") AS date,
                  Failed.NotConnected,
                  count(id) as calls,
                  sum(`duration`)/60 as minutes
   FROM test.`CDR_Vendors`
       inner join Vendors on (CDR_Vendors.i_vendor = Vendors.i_vendor)
       inner join Customers on (CDR_Vendors.i_customer = Customers.i_customer)
       inner join
          (SELECT        
               date_format(connect_time,"%Y-%m-%d %H") AS date,
               Count(id) as NotConnected
               FROM `CDR_Vendors_Failed`
               inner join Vendors on (CDR_Vendors_Failed.i_vendor = Vendors.i_vendor)
               inner join Customers on (CDR_Vendors_Failed.i_customer = Customers.i_customer)
               WHERE
               Customers.name like "W%"
               and
               Vendors.name like "D%"
               and connect_time between curdate() and now()
               GROUP by date
               ORDER BY date
            )Failed on Failed.date = date_format(connect_time,"%Y-%m-%d %H")
       WHERE
       Customers.name like "W%"
       and
       Vendors.name like "D%"
       and connect_time between curdate() and now()
       GROUP by date
       ORDER BY date