如何正确连接表并使用组来汇总MySQL中的数据?

时间:2010-07-01 20:09:32

标签: sql mysql

我在MySQL工作并且在构建一个将汇总两个表中的列的查询时遇到一些麻烦。我想比较每天的请求数量,包含每小时记录的表格和每天包含每日汇总的表格。理想情况下,每个的总和是相同的。

这是架构:

每小时表:

CREATE TABLE requests_hourly (
 customer_id INT,
 date DATETIME,
 requests BIGINT,
 req_type SMALLINT );

每日表

CREATE TABLE requests_daily (
 customer_id INT,
 date DATE,
 requests BIGINT,
 req_type SMALLINT );

不使用SQL来获取所有请求,通过2010年6月的两个表中的req_type

SELECT
 SUM(h.requests),
 SUM(d.requests),
 h.req_type
FROM requests_hourly h
LEFT OUTER JOIN requests_daily d ON d.req_type = h.req_type
WHERE h.date >= '2010-06-01 00:00:00'
 AND h.date < '2010-07-01 00:00:00'
 AND d.date >= '2010-06-01 00:00:00'
 AND d.date < '2010-07-01 00:00:00'
GROUP BY h.req_type;

我感觉错误在JOIN中。提前感谢您的帮助!

答案

我赞同彼得的答案,但确实需要一点修改。所以这是MySQL SQL代码:

SELECT *
FROM
  (SELECT SUM(requests) AS 'Daily Request Sum', req_type
   FROM requests_daily
   WHERE date BETWEEN '2010-06-01 00:00:00' AND '2010-07-01 00:00:00'
   GROUP BY req_type, date) d
  INNER JOIN
  (SELECT SUM(requests) AS 'Hourly Request Sum', req_type
   FROM requests_hourly
   WHERE date BETWEEN '2010-06-01 00:00:00' AND '2010-07-01 00:00:00'
   GROUP BY req_type, DATE(date)) h
USING (req_type, date)

3 个答案:

答案 0 :(得分:0)

我会使用子查询。

SELECT `req_type`
     , `date`
     , COUNT(0) AS `daily_data_count`
     , (
           SELECT COUNT(0)
             FROM `requests_hourly`
            WHERE CAST(`date` AS DATE)
                = `requests_daily`.`date`
       ) AS `hourly_data_count`
  FROM `requests_daily`
 WHERE `date` BETWEEN '2010-06-01' AND '2010-06-30'
 GROUP BY `req_type`, `date`

答案 1 :(得分:0)

您是否考虑过使用 union all 解决问题。

select
  sum(requests) total,
  req_type
from
  requests_daily
where
 requests_daily.date >= '2010-06-01 00:00:00' AND requests_daily.date < '2010-07-01 00:00:00' 
group by requests_daily.req_type
union all
select
  sum(requests) total,
  req_type
from
  requests_hourly
where
 requests_hourly.date >= '2010-06-01 00:00:00' AND requests_hourly.date < '2010-07-01 00:00:00' 
group by requests_hourly.req_type

享受!

答案 2 :(得分:0)

根据我的查询分析器,这是目前为止提出的最快/最有效的方法:

SELECT *
FROM
  (SELECT SUM(requests) AS 'Daily Request Sum', req_type
   FROM requests_daily
   WHERE date BETWEEN '2010-06-01 00:00:00' AND '2010-07-01 00:00:00'
   GROUP BY req_type)
  INNER JOIN
  (SELECT SUM(requests) AS 'Hourly Request Sum', req_type
   FROM requests_hourly
   WHERE date BETWEEN '2010-06-01 00:00:00' AND '2010-07-01 00:00:00'
   GROUP BY req_type)
USING (req_type, date)

它允许简单的逻辑,通过限制大小并在操作发生之前预先连接的表中的索引,显着减少了通过将两个表(req_type)的索引相互匹配而引起的相对“昂贵”的窃听行为。因为你没有唯一的索引(req_type),它会强制数据库执行搜索而不是搜索。

希望这有用。