gpt4 book ai didi

php - 1 周的每日平均值

转载 作者:行者123 更新时间:2023-11-29 00:27:53 26 4
gpt4 key购买 nike

我试图在下面的查询中获取 1 周内每一天的日平均值。

SELECT AVG(`Carbs`) As Carbs, AVG(`Sugar`) As Sugar, AVG(`Units`) As Units,
DATE_FORMAT(`TrackedOn`, '%m/%d/%Y') As Date
FROM `Tracking`
WHERE `TrackedOn` BETWEEN ADDDATE(NOW(), INTERVAL -7 DAY) AND NOW()

但是,它只返回 1 天的平均值(但我认为它是那 1 周内所有内容的平均值,并且只显示第一天)

你能帮我实现这个吗?

这是一些虚拟数据,具有表结构:

--
-- Table structure for table `Tracking`
--

DROP TABLE IF EXISTS `Tracking`;
CREATE TABLE IF NOT EXISTS `Tracking` (
`TrackID` bigint(20) NOT NULL AUTO_INCREMENT,
`UserID` bigint(20) NOT NULL,
`TrackType` int(11) NOT NULL,
`Carbs` decimal(10,3) NOT NULL,
`Sugar` decimal(10,3) NOT NULL,
`Units` decimal(10,3) NOT NULL,
`TrackedOn` datetime NOT NULL,
`TrackLocation` geometry NOT NULL,
PRIMARY KEY (`TrackID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

--
-- Dumping data for table `Tracking`
--

INSERT INTO `Tracking` (`TrackID`, `UserID`, `TrackType`, `Carbs`, `Sugar`, `Units`, `TrackedOn`, `TrackLocation`) VALUES
(1, 3, 1, 36.000, 217.000, 9.343, '2013-08-06 17:38:37', ''),
(2, 3, 1, 36.000, 316.000, 12.171, '2013-08-06 17:40:23', ''),
(3, 3, 4, 12.000, 316.000, 7.671, '2013-08-06 17:42:24', ''),
(4, 3, 5, 0.000, 316.000, 6.171, '2013-08-06 18:01:53', ''),
(5, 3, 5, 0.000, 317.000, 6.200, '2013-08-07 08:25:06', ''),
(6, 3, 2, 89.000, 317.000, 21.033, '2013-08-07 08:25:13', ''),
(7, 3, 5, 0.000, 317.000, 6.200, '2013-08-07 08:27:19', ''),
(8, 3, 3, 82.000, 168.000, 12.193, '2013-08-07 08:27:31', ''),
(9, 1, 3, 86.000, 217.000, 13.000, '2013-08-07 08:28:08', ''),
(10, 3, 5, 0.000, 317.000, 6.200, '2013-08-07 09:13:39', '');

我的目标是返回今天 (NOW()) 和 7 天前的CarbsSugarUnits 的每日平均值.

最佳答案

只需将 GROUP BY DATE_FORMAT(TrackedOn, '%m/%d/%Y') 添加到查询的末尾,MySQL 将按天汇总所有内容:

SELECT AVG(`Carbs`) As Carbs, AVG(`Sugar`) As Sugar, AVG(`Units`) As Units,
DATE_FORMAT(`TrackedOn`, '%m/%d/%Y') As Date
FROM `Tracking`
WHERE `TrackedOn` BETWEEN ADDDATE(NOW(), INTERVAL -7 DAY) AND NOW()
GROUP BY DATE_FORMAT(TrackedOn, '%m/%d/%Y')

关于php - 1 周的每日平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18104846/

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