简体   繁体   中英

Grouping with mysql

I get information of sales from different shops per day. My first attempt was to use:

SELECT DATE(TimeStamp) as DATE,SUM(Sales) FROM gain Group by DATE(TimeStamp)

what works fine.

Now I need to know the results of each shop within that day. I need to get the shop_name somewhere in.

The next is to show only the last 7 days ( INTERVAL 7 DAY ), but how and where?

If shop_name column is in gain table , query will be like below.

SELECT DATE(TimeStamp) as DATE,
SUM(Sales) as total_sales, shop_name
FROM gain 
WHERE DATE(TimeStamp) >=  DATE_SUB(CURDATE(), INTERVAL 7 DAY);
GROUP BY DATE, shop_name;

This query make INTERVAL 7 DAY & if you want to select shop_name somewhere in the table you need to JOIN with this table.

SELECT DATE(TimeStamp) as DATE,
SUM(Sales) as total_sales
FROM gain 
WHERE DATE(TimeStamp) >= NOW() - INTERVAL 7 DAY
GROUP BY DATE(TimeStamp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM