我想使用select
mysql显示data_order
是时间戳
首先只包括最后7行(包含列price
),仅包括今天的每一天,包括今天的每一天。假设我每天(过去7天)单个订单很简单,但是当我在同一天有更多订单时我不知道如何制作mysql语句
+-------------------------------------------------------------+
| table name `comanda` |
+-------------------------------------------------------------+
| id_comanda | keyUnique_comanda | pret_comanda| data_comanda |
+------------+-------------------+-------------+--------------+
| 1 | a1234 | 150 | today |
+------------+-------------------+-------------+--------------+
| 2 | b4321 | 300 | yesturday |
+------------+-------------------+-------------+--------------+
| 3 | b4321 | 200 | yesturday |
+------------+-------------------+-------------+--------------+
| 4 | c4321 | 100 | yesturday |
+------------+-------------------+-------------+--------------+
| 5 | d234 | 50 | 3 day ago |
+------------+-------------------+-------------+--------------+
| 6 | e453 | 200 | 9 day ago |
+------------+-------------------+-------------+--------------+
我尝试了一些东西......但是没有给我看好
SELECT DAY(data_comanda) AS period, SUM(pret_comanda) AS total
FROM comanda
WHERE data_comanda >= CURDATE() + INTERVAL 1 WEEK
GROUP BY period
-- --------------------------------------------------------
--
-- Table structure for table `comanda`
--
CREATE TABLE `comanda` (
`id_comanda` int(11) NOT NULL,
`pret_comanda` varchar(255) NOT NULL,
`pret_comanda_total` int(11) NOT NULL,
`comanda` text NOT NULL,
`nume_comanda` varchar(255) NOT NULL,
`prenume_comanda` varchar(255) NOT NULL,
`email_comanda` varchar(255) NOT NULL,
`telefon_comanda` varchar(19) NOT NULL,
`judet_comanda` varchar(255) NOT NULL,
`adresa_comanda` varchar(2555) NOT NULL,
`clientIP` varchar(255) NOT NULL,
`localitate_comanda` varchar(255) NOT NULL,
`detalii_comanda` varchar(3000) CHARACTER SET utf8 NOT NULL,
`data_comanda` timestamp NOT NULL,
`data_comanda_finalizare` datetime DEFAULT NULL,
`produse_diferite_comanda` varchar(10) NOT NULL,
`keyUnique_comanda` varchar(25) NOT NULL,
`status_comanda` varchar(255) NOT NULL DEFAULT 'asteptare',
`admin_validare_comanda` varchar(255) NOT NULL DEFAULT 'neModificat'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
答案 0 :(得分:2)
而不是
WHERE data_comanda >= CURDATE() + INTERVAL 1 WEEK
你应该使用
WHERE data_comanda >= CURDATE() - INTERVAL 1 WEEK
但这包括今天和过去7天(总共8天)。如果您只需要7天,则应使用
WHERE data_comanda >= CURDATE() - INTERVAL 6 DAY
查询现在将返回
+--------+-------+
| period | total |
+--------+-------+
| 1 | 600 |
| 2 | 150 |
| 27 | 50 |
+--------+-------+
http://rextester.com/QSDUY96504
这可能会造成混淆,因为DAY(data_comanda)
会返回该月的某一天。因此,您可能希望使用DATE
代替DAY
,并获得如下结果:
+------------+-------+
| period | total |
+------------+-------+
| 27.02.2017 | 50 |
| 01.03.2017 | 600 |
| 02.03.2017 | 150 |
+------------+-------+
http://rextester.com/XOECI72414
要包含缺失的日期,您可以使用0到6之间的数字创建内联表
SELECT *
FROM (
SELECT 0 as diff UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6
) diffs
现在LEFT JOIN
你的桌子
SELECT CURDATE() - INTERVAL diffs.diff DAY AS period,
COALESCE(SUM(pret_comanda), 0) AS total
FROM (
SELECT 0 as diff UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 UNION ALL
SELECT 6
) diffs
LEFT JOIN comanda c ON DATE(c.data_comanda) = CURDATE() - INTERVAL diffs.diff DAY
GROUP BY period;
请注意,SUM(pret_comanda)
将在缺失的日期返回NULL。要将其转换为0
,您可以使用COALESCE(SUM(pret_comanda), 0)
现在结果看起来像
+------------+-------+
| period | total |
+------------+-------+
| 24.02.2017 | 0 |
| 25.02.2017 | 0 |
| 26.02.2017 | 0 |
| 27.02.2017 | 50 |
| 28.02.2017 | 0 |
| 01.03.2017 | 600 |
| 02.03.2017 | 150 |
+------------+-------+