我有一个问题是我无法从每一天的表格结果中获取每行的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 |
------------------------------------
当我编写代码php以使Date_id
进行编辑但仍显示第1行时,每列只有一个id相同而第二行,第三行......也只显示一个id。我的表格是这样的:
------------------------------------------------------------------------------------------
| 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 我该如何编写查询?谢谢你。
答案 0 :(得分:0)
您的查询看起来不错但有一个小问题。您正在使用保留关键字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_id
。
<强> DEMO 强>