让我们做你想做的事:
SELECT
WEEKDAY(`datetime_field`) AS `week_day`,
COUNT(*) AS `sale_count`
FROM `orders`
WHERE YEARWEEK(`datetime_field`) = YEARWEEK(NOW())
GROUP BY `week_day`
ORDER BY `week_day` ASC;
这将返回一组带有week_day
和的记录sale_count
。 在这里了解更多。如果您使用NOW()
本地日期时间或使用UTC_TIMESTAMP()
GMT 播放,请使用。
请记住,我不知道您的数据库名称或字段名称。你需要填写这些。
工作示例:
CREATE TABLE `orders` (
`OrderID` int(11) NOT NULL AUTO_INCREMENT,
`OrderDate` datetime NOT NULL,
`OrderValue` decimal(7,2) unsigned NOT NULL,
PRIMARY KEY (`OrderID`)
);
INSERT INTO `orders` VALUES ('1', '2012-10-29 14:02:19', '100.00');
INSERT INTO `orders` VALUES ('2', '2012-10-30 14:02:19', '123.00');
INSERT INTO `orders` VALUES ('3', '2012-10-31 14:02:19', '103.00');
INSERT INTO `orders` VALUES ('4', '2012-11-01 14:02:19', '232.00');
INSERT INTO `orders` VALUES ('5', '2012-11-02 14:02:19', '321.00');
INSERT INTO `orders` VALUES ('6', '2012-11-03 14:02:19', '154.00');
INSERT INTO `orders` VALUES ('7', '2012-11-04 14:02:19', '112.00');
INSERT INTO `orders` VALUES ('8', '2012-10-29 14:02:19', '100.00');
SELECT
WEEKDAY(`OrderDate`) AS `week_day`,
COUNT(*) AS `sales_count`,
SUM(`OrderValue`) AS `sales_value`
FROM `orders`
WHERE YEARWEEK(`OrderDate`) = YEARWEEK(NOW())
GROUP BY `week_day`
ORDER BY `week_day` ASC;
这是创建表的 SQL,本周每天添加 1 个订单,但在星期一添加 2 个订单。以及获取报告的查询。
这是 SQLFIDDLE.COM示例。