将日期显示为列别名

时间:2019-03-02 10:23:28

标签: mysql

我的专栏如下所示。

table

我需要日期作为列别名,例如

02/1/2019, 02/2/2019, 02/3/2019, 02/5/2019, 02/6/2019 etc

2 个答案:

答案 0 :(得分:0)

您需要先转换为日期类型(STR_TO_DATE),然后再转换为所需格式的字符串(DATE_FORMAT)

STR_TO_DATE(t.date, '%c/%e/%Y'), DATE_FORMAT(STR_TO_DATE(tdate, '%c/%e/%Y'), '%m/%e/%Y')

答案 1 :(得分:0)

SET @today = CURRENT_DATE(),
@day1 = DATE_ADD(CURRENT_DATE(),INTERVAL -1 DAY),
@day2 = DATE_ADD(CURRENT_DATE(),INTERVAL -2 DAY),
@day3 = DATE_ADD(CURRENT_DATE(),INTERVAL -3 DAY),
@day4 = DATE_ADD(CURRENT_DATE(),INTERVAL -4 DAY),
@day5 = DATE_ADD(CURRENT_DATE(),INTERVAL -5 DAY),
@sql = concat("SELECT JRR_RESULT, COUNT(LEFT(JRR_RESULT,40)) AS 'Total Error 
Last 
5 Days'"
", sum(IF( DATE_FORMAT(CREATION_DATE,'%Y-%m-%d') = '" , @day1, "',1,0) ) AS 
'", 
@day1, "' "
", sum(IF( DATE_FORMAT(CREATION_DATE,'%Y-%m-%d') = '" , @day2, "',1,0) ) AS 
'", 
@day2, "' "
", sum(IF( DATE_FORMAT(CREATION_DATE,'%Y-%m-%d') = '" , @day3, "',1,0) ) AS 
'", 
@day3, "' "
", sum(IF( DATE_FORMAT(CREATION_DATE,'%Y-%m-%d') = '" , @day4, "',1,0) ) AS 
'", 
@day4, "' "
", sum(IF( DATE_FORMAT(CREATION_DATE,'%Y-%m-%d') = '" , @day5, "',1,0) ) AS 
'", 
@day5, "' "
" FROM your_table where DATE_FORMAT(CREATION_DATE,'%Y-%m-%d') >= '" , @day5, 
"' "
" Group by LEFT(JRR_RESULT,40)"); 

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;