0
我想我的程序裏面如下顯示月份名稱作爲MySQL的列名
year jan Feb ......Dec
---- ----- ----- -----
2016 val1 val2 val3
2017 val4 val5 val6
顯示年月名稱作爲兩個日期之間的列名有一個人幫我做這件事
我想我的程序裏面如下顯示月份名稱作爲MySQL的列名
year jan Feb ......Dec
---- ----- ----- -----
2016 val1 val2 val3
2017 val4 val5 val6
顯示年月名稱作爲兩個日期之間的列名有一個人幫我做這件事
你可以做這樣的事情,如果我理解你問題正確生成所需的結果集。 TIMESTAMPDIFF(DAY.... + INTERVAL 1 MONTH)
可以計算一個月內的天數。 請注意,每個TIMESTAMPDIFF(DAY, '2016-01-01', '2016-01-01' + INTERVAL 1 MONTH) AS 'Jan'
行必須正確的月份,它應該工作。
查詢
SELECT
'2016' AS YEAR
, TIMESTAMPDIFF(DAY, '2016-01-01', '2016-01-01' + INTERVAL 1 MONTH) AS 'Jan'
, TIMESTAMPDIFF(DAY, '2016-02-01', '2016-02-01' + INTERVAL 1 MONTH) AS 'Feb'
...
, TIMESTAMPDIFF(DAY, '2016-12-01', '2016-12-01' + INTERVAL 1 MONTH) AS 'Dec'
UNION ALL
SELECT
'2017' AS YEAR
, TIMESTAMPDIFF(DAY, '2017-01-01', '2017-01-01' + INTERVAL 1 MONTH) AS 'Jan'
, TIMESTAMPDIFF(DAY, '2017-02-01', '2017-02-01' + INTERVAL 1 MONTH) AS 'Feb'
...
, TIMESTAMPDIFF(DAY, '2017-12-01', '2017-12-01' + INTERVAL 1 MONTH) AS 'Dec'
結果
year Jan Feb Dec
------ ------ ------ --------
2016 31 29 31
2017 31 28 31
或者此查詢使其eazier到新的一年,少代碼重複添加。
查詢
SELECT
years.year
, TIMESTAMPDIFF(DAY, CONCAT(years.year, '-01-01'), CONCAT(years.year, '-01-01') + INTERVAL 1 MONTH) AS 'Jan'
, TIMESTAMPDIFF(DAY, CONCAT(years.year, '-02-01'), CONCAT(years.year, '-02-01') + INTERVAL 1 MONTH) AS 'Feb'
...
, TIMESTAMPDIFF(DAY, CONCAT(years.year, '-12-01'), CONCAT(years.year, '-12-01') + INTERVAL 1 MONTH) AS 'Dec'
FROM (
SELECT
'2016' AS YEAR
UNION
ALL
SELECT
'2017' AS YEAR
)
AS years
結果
year Jan Feb Dec
------ ------ ------ --------
2016 31 29 31
2017 31 28 31
提供一些樣本表數據。 – Wanderer
值是月內的天數 – HariPriya
這稱爲* pivoting。*知道這可能會幫助您做更多的研究。可悲的是,這是MySQL脖子上的一個臭名昭着的痛苦。 –