gpt4 book ai didi

PHP & MySQL : Query to "sum" 2 rows from PHP

转载 作者:行者123 更新时间:2023-11-29 03:02:09 25 4
gpt4 key购买 nike

我有下表:

+------------+-------+------------+-----------+
| date | time | custom_id | Number |
+------------+-------+------------+-----------+
| 01/17/2014 | 00:00 | ABC123 | 15 |
| 01/17/2014 | 00:00 | HEY567 | 25 |
| 01/17/2014 | 00:30 | ABC123 | 17 |
| 01/17/2014 | 00:30 | HEY567 | 28 |
| 01/17/2014 | 01:00 | ABC123 | 17 |
| 01/17/2014 | 01:00 | HEY567 | 28 |
| 01/17/2014 | 01:30 | ABC123 | 17 |
| 01/17/2014 | 01:30 | HEY567 | 28 |
+------------+-------+------------+-----------+

如何对具有相同CUSTOM_IDDATE 的 2 行(数字)求和,并且给定时间仅为:30 分钟将它四舍五入到 :00 并删除 :30?它最终应该是这样的:

+------------+-------+------------+-----------+
| date | time | custom_id | Number |
+------------+-------+------------+-----------+
| 01/17/2014 | 00:00 | ABC123 | 32 |
| 01/17/2014 | 00:00 | HEY567 | 53 |
| 01/17/2014 | 01:00 | ABC123 | 34 |
| 01/17/2014 | 01:00 | HEY567 | 56 |
+------------+-------+------------+-----------+

我如何使用 PHP 或可能使用 MySQL 查询来完成此操作?

SQLFiddle:http://sqlfiddle.com/#!2/cc529/1

最佳答案

这应该可行

SELECT date, MIN(time) as time, custom_id, SUM(Number) as Number
FROM table
GROUP BY date, custom_id, HOUR(time)

关于PHP & MySQL : Query to "sum" 2 rows from PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21663332/

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