gpt4 book ai didi

php - 使用具有多个时间范围的单个查询从两个表中获取数据

转载 作者:行者123 更新时间:2023-11-29 12:53:22 24 4
gpt4 key购买 nike

我有两个表reportreport1

报告

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

文件.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

如何在单个查询中执行此操作

最佳答案

我想我会使用您当前的查询作为子查询并将其与另一个表连接(因为您当前的查询生成另一个表所键入的时间范围名称)。

类似这样的事情,尽管您还需要将日期添加到子查询中(不确定您要加入的日期 - 所有记录都来自同一日期吗?):-

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

关于php - 使用具有多个时间范围的单个查询从两个表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24433123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com