MySQL Select Query无法连续获取每天的id

MySQL Select Query无法连续获取每天的id,mysql,select,Mysql,Select,我有一个问题是,我无法从每一行的表结果中获取每天的id。我在phpMyAdmin中的表如下所示: ------------------------------------ | Date_id | Date | ------------------------------------ | 1 | 2014-05-13 | | 2 | 2014-06-04 | |

我有一个问题是,我无法从每一行的表结果中获取每天的id。我在phpMyAdmin中的表如下所示:

  ------------------------------------
  |   Date_id  |        Date         |
  ------------------------------------
  |    1       |  2014-05-13         |
  |    2       |  2014-06-04         |
  |    3       |  2014-07-09         |
  |    4       |  2014-08-13         |
  |    5       |  2014-09-12         |
  |    6       |  2014-10-15         |
  |    7       |  2014-11-19         |
  |    8       |  2014-12-10         |
  |    9       |  2015-01-14         |
  |    10      |  2015-02-11         |
  |    11      |  2015-03-10         |
  |    12      |  2015-04-15         |
  |    13      |  2015-05-12         |
  |    14      |  2015-06-12         |
  ------------------------------------
------------------------------------------------------------------------------------------
|   Year  | Jan | Feb | Mar | April | May | June | July | Aug | Sept | Oct | Nov | Dec |
------------------------------------------------------------------------------------------
| 2014    |     |     |     |       | 13  | 04   | 07   | 13  | 12   | 15  | 19  | 10  |
----------------------------------------------------------------------------------------
| 2015    | 14  | 11  | 10  |  15   | 12  | 12   |      |     |      |     |     |     |
----------------------------------------------------------------------------------------
当我编写代码php以获取要编辑的
Date\u id
时,它仍然显示第一行,每列只有一个id,第二行,第三行是相同的。。。也仅显示一个id。我的表格如下所示:

  ------------------------------------
  |   Date_id  |        Date         |
  ------------------------------------
  |    1       |  2014-05-13         |
  |    2       |  2014-06-04         |
  |    3       |  2014-07-09         |
  |    4       |  2014-08-13         |
  |    5       |  2014-09-12         |
  |    6       |  2014-10-15         |
  |    7       |  2014-11-19         |
  |    8       |  2014-12-10         |
  |    9       |  2015-01-14         |
  |    10      |  2015-02-11         |
  |    11      |  2015-03-10         |
  |    12      |  2015-04-15         |
  |    13      |  2015-05-12         |
  |    14      |  2015-06-12         |
  ------------------------------------
------------------------------------------------------------------------------------------
|   Year  | Jan | Feb | Mar | April | May | June | July | Aug | Sept | Oct | Nov | Dec |
------------------------------------------------------------------------------------------
| 2014    |     |     |     |       | 13  | 04   | 07   | 13  | 12   | 15  | 19  | 10  |
----------------------------------------------------------------------------------------
| 2015    | 14  | 11  | 10  |  15   | 12  | 12   |      |     |      |     |     |     |
----------------------------------------------------------------------------------------
这是我使用它的查询代码:

select year(`Date`) as `year`,Date_id,
   max(case when month(date) = 1 then day(`date`) end) as Jan,
   max(case when month(date) = 2 then day(`date`) end) as Feb,
   max(case when month(date) = 3 then day(`date`) end) as Mar,
   max(case when month(date) = 4 then day(`date`) end) as Apr,
   max(case when month(date) = 5 then day(`date`) end) as May,
   max(case when month(date) = 6 then day(`date`) end) as Jun,
   max(case when month(date) = 7 then day(`date`) end) as Jul,
   max(case when month(date) = 8 then day(`date`) end) as Aug,
   max(case when month(date) = 9 then day(`date`) end) as Sep,
   max(case when month(date) = 10 then day(`date`) end) as Oct,
   max(case when month(date) = 11 then day(`date`) end) as Nov,
   max(case when month(date) = 12 then day(`date`) end) as Dec
from table t
group by year(date)
order by year(date)
我的预期结果是,它将显示为我的表格,并将获得每天的id。

如何编写查询?谢谢。

您的查询看起来不错,但有一个小问题。您正在使用一个保留关键字
dec
,它正在中断查询。您需要反勾选它,这是修改后的查询

select year(`Date`) as `year`,Date_id,
   max(case when month(date) = 1 then day(`date`) end) as Jan,
   max(case when month(date) = 2 then day(`date`) end) as Feb,
   max(case when month(date) = 3 then day(`date`) end) as Mar,
   max(case when month(date) = 4 then day(`date`) end) as Apr,
   max(case when month(date) = 5 then day(`date`) end) as May,
   max(case when month(date) = 6 then day(`date`) end) as Jun,
   max(case when month(date) = 7 then day(`date`) end) as Jul,
   max(case when month(date) = 8 then day(`date`) end) as Aug,
   max(case when month(date) = 9 then day(`date`) end) as Sep,
   max(case when month(date) = 10 then day(`date`) end) as Oct,
   max(case when month(date) = 11 then day(`date`) end) as Nov,
   max(case when month(date) = 12 then day(`date`) end) as `Dec`
from test t
group by `year`
order by `year`
如果不需要,您可以从选择列表中提取
Date\u id


我想您需要在
group by
条款中使用
Date\u id
,您可以举个例子。我的意思是,您如何在我的查询中添加。请添加它,因为我写它时出错。只需在group by子句上再次查询“添加代码me”。为什么2014年7月表中的值是07,而不是09?如果没有,那么背后的逻辑是什么?@JonhKevin在你的查询中有
date\u id
,但你我看不见,然后是
。因此,添加
并按
日期分组\u id
这没有什么害处,但是这个特定的顺序是多余的。啊,我明白了@草莓的意思,我没有注意到,
年(日期)作为年已在选择中,在顺序中添加多余的代码是没有意义的。让我更新一下,顺便说一句,好的捕捉和非常敏锐的观察。