我有一个查询,用于检查具有多个时间范围的mysql中两个不同表的用户数和应用程序数。
我用来从两个具有不同时间范围的表中获取记录的查询是巨大而静态的。报告生成如下
Time count Logged In user Count-Apps
5.30-6.30 PM 2 3
6.31-7.30 PM 4 1
7.31 - 8.30 PM 3 2
8.31-9.30 PM 5 7
9.31-10.30 PM 2 2
10.31-11.30 PM 3 4
11.31- 12.30 PM 5 5
我的script.php
<?php
$query=mysql_query("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:31:00'), '%Y-%m-%d %H:%i:%s')
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '08:31:00' and '09:30:00'
THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '08:31:00'), '%Y-%m-%d %H:%i:%s')
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '09:31:00' and '10:30:00'
THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '09:31:00'), '%Y-%m-%d %H:%i:%s')
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '10:31:00' and '11:30:00'
THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '10:31:00'), '%Y-%m-%d %H:%i:%s')
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '11:31:00' and '12:30:00'
THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '11:31: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')
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '08:31:00' and '09:30:00'
THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '09:30:00'), '%Y-%m-%d %H:%i:%s')
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '09:31:00' and '10:30:00'
THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '10:30:00'), '%Y-%m-%d %H:%i:%s')
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '10:31:00' and '11:30:00'
THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '11:30:00'), '%Y-%m-%d %H:%i:%s')
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '11:31:00' and '12:30:00'
THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '12: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:31:00-08:30:00'
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '08:31:00' and '09:30:00'
THEN '08:31:00-09:30:00'
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '09:31:00' and '10:30:00'
THEN '09:31:00-10:30:00'
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '10:31:00' and '11:30:00'
THEN '10:31:00-11:30:00'
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '11:31:00' and '12:30:00'
THEN '11:31:00-12: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");
echo'<html>
<head>
<title>Count User Info TimeWise</title>
</head>
<h1>Count User</h1>
<table border="3" cellspacing="2">
<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 "<td>" . $row['countapps'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</html>";
在上面的查询中,它加入了两个表report
和report1
并且它的工作正常,但它不是传统的做法,因为将来如果添加时间范围我必须再次添加它的额外行和查询将变得更大。
如何通过获取数组并将所有值放在那里然后循环并运行sql来使其动态化。
答案 0 :(得分:0)
您可以使用PHP构建查询:
$time_ranges = array(
array('05:30:00', '06:30:00'),
array('06:31:00', '07:30:00'),
array('07:31:00', '08:30:00'),
array('08:31:00', '09:30:00'),
array('09:31:00', '10:30:00'),
array('10:31:00', '11:30:00'),
array('11:31:00', '12:30:00'),
);
$sql="SELECT sub0.TimeRange, sub0.number, COUNT(*) AS countapps
FROM
(
SELECT
CASE
";
foreach ($time_ranges as $r) {
$sql .= "
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '$r[0]' and '$r[1]'
THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '$r[0]'), '%Y-%m-%d %H:%i:%s') ";
}
$sql .= "
ELSE NULL
END AS StartRange,
CASE ";
foreach ($time_ranges as $r) {
$sql .= "
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '$r[0]' and '$r[1]'
THEN STR_TO_DATE(CONCAT(CURDATE(), ' ', '$r[1]'), '%Y-%m-%d %H:%i:%s') ";
}
$sql .= "
ELSE NULL
END AS EndRange,
CASE ";
foreach ($time_ranges as $r) {
$sql .= "
WHEN DATE_FORMAT(dt,'%H:%i:%s') BETWEEN '$r[0]' and '$r[1]'
THEN '$r[0]-$r[1]' ";
}
$sql .= "
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";
$query=mysql_query($sql);