使用数据库中的数据显示每日增长
Displaying the daily increase using data from the database
我有一个 table,我在其中存储我使用 PHP 从 API 获得的数据,table 看起来像这样:
ID
count
timestamp
6285
123
21.11 18:54
6284
122
21.11 18:53
6283
121
21.11 18:52
6282
120
21.11 18:51
我想要实现的是:在过去的 24 小时内,数字增加了 X
要从我使用的数据库中检索数据:
while($row = mysqli_fetch_array($result))
{
// generating HTML table etc
// echo-ign data with $row['timestamp']
}
我的想法是在24小时内获得第一次写入和最后一次写入并获得差异,但我无法将这个想法转移到PHP
自 v.8.0 以来的现代 MySQL 版本提供了 window functions
这样的 'FIRST_VALUE':
SELECT DISTINCT
DATE(`timestamp`) as day,
FIRST_VALUE(`count`) OVER (PARTITION BY DATE(`timestamp`) ORDER BY `timestamp`) as start_day_count,
FIRST_VALUE(`count`) OVER (PARTITION BY DATE(`timestamp`) ORDER BY `timestamp` DESC) as end_day_count
FROM t;
PHP 解决方案可以是下一个(使用PDO模块):
<?php
$query = "
SELECT DISTINCT
FIRST_VALUE(`count`) OVER (PARTITION BY DATE(`timestamp`) ORDER BY `timestamp`) as start_day_count,
FIRST_VALUE(`count`) OVER (PARTITION BY DATE(`timestamp`) ORDER BY `timestamp` DESC) as end_day_count
FROM t WHERE DATE(`timestamp`) = '2021-11-21';";
$stmt = $pdo->prepare($query);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($row);
在这里你可以test PHP with MySQL window functions
或使用 mysqli:
<?php
$query = "
SELECT DISTINCT
FIRST_VALUE(`count`) OVER (PARTITION BY DATE(`timestamp`) ORDER BY `timestamp`) as start_day_count,
FIRST_VALUE(`count`) OVER (PARTITION BY DATE(`timestamp`) ORDER BY `timestamp` DESC) as end_day_count
FROM t WHERE DATE(`timestamp`) = '2021-11-21';";
$result = mysqli_query($mysqli, $query);
$row = mysqli_fetch_array($result);
print_r($row);
我们可以说“我想要过去 24 小时的最大计数和最小计数之间的差值”吗?计数总是在增加吗?
建议:
SELECT MAX(count) - MIN(count) AS up_24h
FROM your_table
WHERE timestamp >= NOW() - INTERVAL (24 * 60) MINUTE
我有一个 table,我在其中存储我使用 PHP 从 API 获得的数据,table 看起来像这样:
ID | count | timestamp |
---|---|---|
6285 | 123 | 21.11 18:54 |
6284 | 122 | 21.11 18:53 |
6283 | 121 | 21.11 18:52 |
6282 | 120 | 21.11 18:51 |
我想要实现的是:在过去的 24 小时内,数字增加了 X
要从我使用的数据库中检索数据:
while($row = mysqli_fetch_array($result))
{
// generating HTML table etc
// echo-ign data with $row['timestamp']
}
我的想法是在24小时内获得第一次写入和最后一次写入并获得差异,但我无法将这个想法转移到PHP
自 v.8.0 以来的现代 MySQL 版本提供了 window functions
这样的 'FIRST_VALUE':
SELECT DISTINCT
DATE(`timestamp`) as day,
FIRST_VALUE(`count`) OVER (PARTITION BY DATE(`timestamp`) ORDER BY `timestamp`) as start_day_count,
FIRST_VALUE(`count`) OVER (PARTITION BY DATE(`timestamp`) ORDER BY `timestamp` DESC) as end_day_count
FROM t;
PHP 解决方案可以是下一个(使用PDO模块):
<?php
$query = "
SELECT DISTINCT
FIRST_VALUE(`count`) OVER (PARTITION BY DATE(`timestamp`) ORDER BY `timestamp`) as start_day_count,
FIRST_VALUE(`count`) OVER (PARTITION BY DATE(`timestamp`) ORDER BY `timestamp` DESC) as end_day_count
FROM t WHERE DATE(`timestamp`) = '2021-11-21';";
$stmt = $pdo->prepare($query);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
print_r($row);
在这里你可以test PHP with MySQL window functions
或使用 mysqli:
<?php
$query = "
SELECT DISTINCT
FIRST_VALUE(`count`) OVER (PARTITION BY DATE(`timestamp`) ORDER BY `timestamp`) as start_day_count,
FIRST_VALUE(`count`) OVER (PARTITION BY DATE(`timestamp`) ORDER BY `timestamp` DESC) as end_day_count
FROM t WHERE DATE(`timestamp`) = '2021-11-21';";
$result = mysqli_query($mysqli, $query);
$row = mysqli_fetch_array($result);
print_r($row);
我们可以说“我想要过去 24 小时的最大计数和最小计数之间的差值”吗?计数总是在增加吗?
建议:
SELECT MAX(count) - MIN(count) AS up_24h
FROM your_table
WHERE timestamp >= NOW() - INTERVAL (24 * 60) MINUTE