如何在mysql中获取每周明智的记录

时间:2019-03-28 18:50:16

标签: mysql

想要每周获取总计价格

select @start:=adddate(Date_of_purchase,interval 2- dayofweek(Date_of_purchase)day),adddate(@start,interval 8- dayofweek(@start)day) as 'week end date',week(Date_of_purchase) as week,price from purchase where Date_of_purchase between '2017-12-16' and '2019-01-29';
+------------------------------------------------------------------------------+---------------+------+-------+
| @start:=adddate(Date_of_purchase,interval 2- dayofweek(Date_of_purchase)day) | week end date | week | price |
+------------------------------------------------------------------------------+---------------+------+-------+
| 2018-01-29                                                                   | 2018-02-04    |    4 |   300 |
| 2018-01-29                                                                   | 2018-02-04    |    4 |    70 |
| 2018-01-29                                                                   | 2018-02-04    |    4 |    70 |
| 2018-02-05                                                                   | 2018-02-11    |    5 |   180 |
| 2018-02-05                                                                   | 2018-02-11    |    5 |   180 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |   190 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |   390 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |   390 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |   180 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |    70 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |    70 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |    70 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |    20 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |    45 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |    70 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |    70 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |    70 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |   300 |
| 2019-01-28                                                                   | 2019-02-03    |    4 |   200 |
+------------------------------------------------------------------------------+---------------+------+-------+
 mysql> select sum(price), @start:=adddate(Date_of_purchase,interval 2- dayofweek(Date_of_purchase)day),adddate(@start,interval 8- dayofweek(@start)day) as 'week end date',week(Date_of_purchase) as week,price from purchase where Date_of_purchase between '2017-12-16' and '2019-01-29' group by @start;
+------------+------------------------------------------------------------------------------+---------------+------+-------+
| sum(price) | @start:=adddate(Date_of_purchase,interval 2- dayofweek(Date_of_purchase)day) | week end date | week | price |
+------------+------------------------------------------------------------------------------+---------------+------+-------+
|        320 | 2018-01-29                                                                   | 2018-02-04    |    4 |    70 |
|        370 | 2018-02-05                                                                   | 2018-02-11    |    5 |   180 |
|       2245 | 2018-01-29                                                                   | 2018-02-04    |    4 |   300 |
+------------+------------------------------------------------------------------------------+---------------+------+-------+
3 rows in set (1.94 sec)

第一行的总价格为440,显示为320

0 个答案:

没有答案