Mysql Sum and Group By Method

2019-09-18 16:42发布

问题:

Input

Date            Points            Email

2012-07-01       5                 a@sample.com
2012-07-01       6                b@sample.com
2012-07-01       2                 c@sample.com

2012-07-02       5                d@sample.com

2012-07-03       8                e@sample.com
2012-07-03       7                 f@sample.com
2012-07-03       1                 y@sample.com

2012-07-04       3                x@sample.com
2012-07-04       2                f@sample.com

2012-07-05       3                g@sample.com
2012-07-05       9                b@sample.com

Output

Date            Points

2012-07-01       13
2012-07-02        5
2012-07-03       16
2012-07-04        5
2012-07-05       12

Suggest me an MySQL Query for output like the above

回答1:

try

 SELECT `date`, SUM(`Points`) AS sumPoint FROM table GROUP BY `date`

Note : dont forget to wrap date column with ` as DATE is reserve keyword of mySQL



回答2:

SELECT date, SUM(Points) AS sumPoint FROM table GROUP BY DATE(date)

Since date column might be datetime or date type in table