[mysql]取两个表使用单个查询有多个时间范围

标签: MySQL PHP
发布时间: 2017/1/9 21:42:21
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有两个表 reportreport1

report

id   user    dt
1    a  2014-06-23 05:30:00
2    b  2014-06-23 06:30:00
3    c  2014-06-23 06:31:00
4    d  2014-06-23 07:30:00

要生成这样报告与多个日期范围我做

range             count     
06:31:00-07:30:00   2
5:30:00-6:30:00     2

file.php

$query=mysql_query("SELECT CASE
        WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '05:30:00' and '06:30:00' and  DATE_FORMAT(dt,'%Y:%m:%d')=DATE(CURDATE())
            THEN '5:30:00-6:30:00'
        WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '06:31:00' and '07:30:00' and DATE_FORMAT(dt,'%Y:%m:%d')=DATE(CURDATE())
            THEN '06:31:00-07:30:00'
        WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '07:31:00' and '08:30:00' and DATE_FORMAT(dt,'%Y:%m:%d')=DATE(CURDATE())
            THEN '07:30:00-08:30:00'
    ELSE NULL
    END AS TimeRange,
    COUNT(*) as number
FROM report
GROUP BY TimeRange
HAVING TimeRange IS NOT NULL");

echo'<html>
<head>
<title>Count User Info TimeWise</title>
</head>
<h1>Count User</h1>
<table border="1" cellspacing="1">

 <tr>
 <th>range</th>
 <th>count</th>
 <th>Apps Count</th>';

 while($row = mysql_fetch_array($query))
  {
echo  "<tr>";
   echo "<td>" . $row['TimeRange'] . "</td>";
   echo "<td>" . $row['number'] . "</td>";
   echo "</tr>";
   }
echo "</table>";      
echo "</html>";
?>

现在我想要生成的报表添加将从另一个表中提取了一更多列report1

range             count         countapps
06:31:00-07:30:00   2             count from report1
5:30:00-6:30:00     2             count from report1

report1

user    dt
a     2014-06-26 05:30:00
b     2014-06-26 06:00:00

在单个查询中,如何能做这个

解决方法 1:

我认为将你当前的查询使用子查询作为加入那个反对另一个表 (如您当前的查询将生成另一个表键的 timerange 名称)。

这样的事情,尽管您将需要将日期添加到子查询以及 (不知道哪一天你想要加入的是从同一个日期的所有记录?):-

SELECT sub0.TimeRange, sub0.number, report1.`count`
FROM
(
    SELECT CASE
            WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '05:30:00' and '06:30:00'
                THEN '5:30:00-6:30:00'
            WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '06:31:00' and '07:30:00'
                THEN '06:31:00-07:30:00'
            WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '07:31:00' and '08:30:00'
                THEN '07:30:00-08:30:00'
            ELSE NULL
        END AS TimeRange,
        COUNT(*) as number
    FROM report
    GROUP BY TimeRange
    HAVING TimeRange IS NOT NULL
) sub0
LEFT OUTER JOIN report1
ON report1.range = sub0.TimeRange

编辑 — — 有点笨拙,但想这样它会:-

SELECT sub0.TimeRange, sub0.number, COUNT(*) AS countapps
FROM
(
    SELECT 
        CASE
            WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '05:30:00' and '06:30:00'
                THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '05:30:00'), '%Y-%m-%d %H:%i:%s')
            WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '06:31:00' and '07:30:00'
                THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '06:31:00'), '%Y-%m-%d %H:%i:%s')
            WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '07:31:00' and '08:30:00'
                THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '07:30:00'), '%Y-%m-%d %H:%i:%s')
            ELSE NULL
        END AS StartRange,
        CASE
            WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '05:30:00' and '06:30:00'
                THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '06:30:00'), '%Y-%m-%d %H:%i:%s')
            WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '06:31:00' and '07:30:00'
                THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '07:30:00'), '%Y-%m-%d %H:%i:%s')
            WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '07:31:00' and '08:30:00'
                THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '08:30:00'), '%Y-%m-%d %H:%i:%s')
            ELSE NULL
        END AS EndRange,
        CASE
            WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '05:30:00' and '06:30:00'
                THEN '05:30:00-06:30:00'
            WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '06:31:00' and '07:30:00'
                THEN '06:31:00-07:30:00'
            WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '07:31:00' and '08:30:00'
                THEN '07:30:00-08:30:00'
            ELSE NULL
        END AS TimeRange,
        COUNT(*) as number
    FROM report
    WHERE DATE_FORMAT(dt,'%Y:%m:%d')=DATE(CURDATE())
    GROUP BY StartRange, EndRange, TimeRange
    HAVING TimeRange IS NOT NULL
) sub0
LEFT OUTER JOIN report1
ON report1.dt BETWEEN sub0.StartRange AND sub0.EndRange
GROUP BY sub0.TimeRange, sub0.number
赞助商