![](/img/trans.png)
[英]How to get the First day and last day of the month from given month and year in sql
[英]How to get last day of month in mysql by giving month and year as input
如何通过提供月份和年份作为输入来获取 MySQL 中每月的最后一天。
Similar example, To get last day of the month by date as input
Eg: SELECT LAST_DAY(?) as lastDate
Format:
Input month and year: '07/2015'
Output Result: day /*Eg: 30,31*/
我尝试使用日期格式SELECT LAST_DAY(DATE_FORMAT('09/2015','%m/%Y')) as lastDate
但它不起作用
您只需要将值转换为日期即可。 这是一种方法:
select last_day(date(concat_ws('-', year, month, 1)))
set @year:=2010;
set @month:=10;
select last_day(concat(@year,'-',@month,'-01')) as last_day;
SELECT LAST_DAY('2020-06-18') AS 'Result';
SELECT LAST_DAY(CURDATE()+ INTERVAL 1 MONTH);
SELECT LAST_DAY(CURDATE() - INTERVAL 1 MONTH);
SELECT DATE_FORMAT(LAST_DAY('2015-9-1'),'%d')
只需在查询中添加任意数字日期(天),然后仅使用日期格式 %d 获取最后一天
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.