我试图在下面的查询中获取 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 天前之间的Carbs
,Sugar
的每日平均值。Units