1

我有一张桌子

| inquiry_id | supplier_id | created             |
 ------------------------------------------------
| 1          | 2           | 2012-10-19 03:10:29 |
 ------------------------------------------------ 
| 2          | 2           | 2012-10-17 04:11:23 |
 ------------------------------------------------
| 3          | 2           | 2012-09-19 03:15:11 |
 ------------------------------------------------
| 4          | 2           | 2012-09-7 01:16:32  |
 ------------------------------------------------

我想要一个 mysql 结果,例如,

| Month - Year | Total Inquiry |
--------------------------------
| 10-2013      | 2             |
--------------------------------
| 09-2013      | 2             |       
--------------------------------

如何使用 MySQL 做到这一点?

4

2 回答 2

6

使用DATE_FORMAT()功能:

SELECT DATE_FORMAT(created,'%m-%Y') AS Month_Year
     , COUNT(inquiry_id) AS Total_Inquiry
FROM Table1
GROUP BY DATE_FORMAT(created,'%m-%Y')

看到这个 SQLFiddle

于 2013-07-10T07:13:44.977 回答
0

你可以使用:

SELECT DATE_FORMAT(created,'%m-%Y'), COUNT(inquiry_id) 
FROM tbl
GROUP BY YEAR(created), MONTH(created)

这是SQLFiddle

于 2013-07-10T07:15:50.890 回答