[英]Show total expense in each category for last SIX months in MYSQL
PLEASE READ THIS BEFORE MARKING AS DUPLICATE 重复标记之前,请先阅读此内容
This is the structure of products table: 这是产品表的结构:
There are more than 600 Rows of record in this table. 该表中有600多条记录。
What I want is that it shows something like this 我想要的是它显示出这样的内容
| Category | Price | Month | Year |
| Bills | 12389 | 08 | 2017 |
| Food | 10589 | 08 | 2017 |
| ... | ..... | .. | .... |
| Bills | 18989 | 07 | 2017 |
| Food | 20589 | 07 | 2017 |
| ... | ..... | .. | .... |
| Bills | 12784 | 06 | 2017 |
| Food | 23589 | 06 | 2017 |
| ... | ..... | .. | .... |
What I did ? 我做了什么 ?
SELECT category AS 'Category', SUM(total_PRICE) AS 'Total Amount'
FROM `products`
WHERE month(DATE)=(SELECT MONTH(CURDATE())) AND year(DATE)=(SELECT YEAR(CURDATE())) GROUP BY category ORDER BY `Total Amount` DESC
This one returns record for current month " Category View " 这一项返回当月的记录“ 类别视图 ”
I also tried this one 我也尝试过这个
SELECT *
FROM products
WHERE date_format(date,'%Y-%m') < date_format(now(),'%Y-%m') and
date_format(date,'%Y-%m') >= date_format(now() - interval 6 month,'%Y-%m')
This returns all record from last SIX months 这将返回过去六个月的所有记录
If it is impossible to write this type of query, then help me writing a program in PHP that will print this type of data to screen 如果无法编写这种类型的查询,请帮我用PHP编写一个程序,该程序会将这种类型的数据打印到屏幕上
You would seem to want: 您似乎想要:
SELECT category, date_format(date,'%Y-%m') as yyyymm, SUM(total_PRICE)
FROM products
WHERE date_format(date,'%Y-%m') < date_format(now(),'%Y-%m') and
date_format(date,'%Y-%m') >= date_format(now() - interval 6 month,'%Y-%m')
GROUP BY category, date_format(date,'%Y-%m')
ORDER BY category, yyyymm DESC;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.