0

I want to create two queries for my table which has fields name,surname and amount paid,the first query should select the day,month and the amount paid,the second query should select a month,year in that year and the total amount paid in that month,lets say john paid on 2013-05-01, on 2013-05-03,while peter paid on 2013-04-08, i want the first query to output

month and day   amount
  05-01          200
  05-03          400
  04-08          50

and the second query should output:

month and year   total
2013-05           600
2013-04            50

I know I can use the sum aggregate function to select the total but the tricky part is how to select the day and the month in the format above,

4

1 回答 1

2

第一个查询将是

SELECT DATE_FORMAT(date, "%m-%d") AS 'month and day',price as amount FROM `tablename` 

第二个查询将是

SELECT DATE_FORMAT(date, "%Y-%m") AS 'month and year' , SUM(price) AS total FROM `tablename` GROUP BY YEAR(date), MONTH(date)
于 2013-05-06T07:36:32.240 回答