数据库
首页 > 数据库> > 从MySQL date_format中的%m和%d中删除前导零

从MySQL date_format中的%m和%d中删除前导零

作者:互联网

以下查询生成一个类似于2016,01,02的日期.如何从月份和日期中删除尾随零,以使其看起来像2016,1,2?

SELECT DATE_FORMAT(earning_created, '%Y, %m, %d') AS day, SUM(earning_amount) AS amount
FROM earnings
WHERE earning_account_id = ?
GROUP BY DATE(earning_created)
ORDER BY earning_created

解决方法:

您可以使用%c格式化月份而不使用前导零和%e来格式化月中的某一天:

SELECT   DATE_FORMAT(earning_created, '%Y, %c, %e') AS day, -- Here! 
         SUM(earning_amount) AS amount
FROM     earnings
WHERE    earning_account_id = ?
GROUP BY DATE(earning_created)
ORDER BY earning_created

标签:mysql,date,sql,select,date-formatting
来源: https://codeday.me/bug/20190609/1201914.html