简体   繁体   中英

MySQL Select Query can not get the id for each day in a row

I have a problem is I can not get the id from each day in table result in each row. My table in phpMyAdmin is like this:

  ------------------------------------
  |   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         |
  ------------------------------------

When I write code php to get the Date_id to edit but it still display the 1st row is only one id for each column the same and the second, third row... is also display only one id. My Tables like this:

------------------------------------------------------------------------------------------
|   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   |      |     |      |     |     |     |
----------------------------------------------------------------------------------------

This is my query code that I had use it:

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)

My Expected result is it will be display as my tables and it will get the id from each day.
How can I write the query? Thanks you.

Your query looks fine but there is a small issue. You are using a reserved key word dec which is breaking the query. You need to backtick it , here is the revised query

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`

You can takeout Date_id from the select list if you dont want.

DEMO

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM