PHP / MYSQL查询有助于分组结果

时间:2023-01-15 03:45:13

I have a MYSQL table with 3 fields: CODE, DATE, COUNT.

CODE is the ID of the client
DATE is the date the entry was added
COUNT is the number of credits used by that client on that date

我有一个MYSQL表,包含3个字段:CODE,DATE,COUNT。 CODE是客户的ID DATE是条目被添加的日期COUNT是该客户在该日期使用的信用数量

I need to be able to know how many credits each client has used on a per month basis. So I need to group the results by client and by year/month.

我需要知道每个客户每月使用多少积分。所以我需要按客户和年/月对结果进行分组。

Basically I need an output that will look something like this :

基本上我需要一个看起来像这样的输出:


CODE     YEAR     MONTH     COUNT
123 2009 07 65
123 2009 06 35
456 2009 07 0
456 2009 06 78
789 2009 07 18

What I have so far is this but it's not really working :

到目前为止我所拥有的只是它,但它并没有真正起作用:

"SELECT CODE, COUNT(CODE), SUM(COUNT), YEAR(DATE), MONTH(DATE) FROM table"

“SELECT CODE,COUNT(CODE),SUM(COUNT),YEAR(DATE),MONTH(DATE)FROM table”

Any help?

1 个解决方案

#1


Not sure if I am over simplyfing here but would it not be

不确定我是不是简单地在这里,但不会

SELECT CODE, COUNT(CODE), SUM(COUNT), YEAR(DATE), MONTH(DATE) FROM tbl GROUP BY CODE, COUNT, YEAR(DATE), MONTH(DATE)

#1


Not sure if I am over simplyfing here but would it not be

不确定我是不是简单地在这里,但不会

SELECT CODE, COUNT(CODE), SUM(COUNT), YEAR(DATE), MONTH(DATE) FROM tbl GROUP BY CODE, COUNT, YEAR(DATE), MONTH(DATE)