MySQL 如何在mysql中找出一个月的天数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10736989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:31:13  来源:igfitidea点击:

how to find out number of days in month in mysql

mysqlsqldate

提问by Fedor Hajdu

I have several dates in mysql tables, using those dates I need to find out the number of days in the month. Suppose i have 2003-02-05 it should return 28. for example

我在 mysql 表中有几个日期,使用这些日期我需要找出当月的天数。假设我有 2003-02-05 它应该返回 28。例如

date                days_in_month
2003-2-3            28

回答by Fedor Hajdu

SELECT DAY(LAST_DAY(yourdate))

回答by Marco

Try this:

尝试这个:

SELECT DAYOFMONTH(LAST_DAY(your_date)) FROM your_table

回答by sumit

You can combine LAST_DAY with string function

您可以将 LAST_DAY 与字符串函数结合使用

SELECT RIGHT( LAST_DAY(  '2003-02-03' ) , 2 )

回答by Cylindric

An alternative to string-chopping is to use:

字符串斩波的替代方法是使用:

SELECT DAY(LAST_DAY('2010-02-1'));

回答by Romil Kumar Jain

Use following statement

使用以下语句

SELECT DAY(LAST_DAY(now()))

回答by Saranya Jothiprakasam

try

尝试

select DAY(LAST_DAY(yourdate)) as days_in_month

选择 DAY(LAST_DAY(yourdate)) 作为 days_in_month

回答by Krishnakumar

I think you are asking the total number of days to be returned for a month. If you are trying to find the total number of days for current month, here is the query:

我想你是在问一个月要返回的总天数。如果您要查找当月的总天数,请执行以下查询:

select timestampdiff(day,
concat(year(now()),'-',month(now()),'-01'),
date_add( concat(year(now()),'-',month(now()),'-01'), interval 1 month)).

If you want to externalize this using any programming language, externalize year and month in the code and replace that with now()

如果您想使用任何编程语言将其具体化,请在代码中具体化年份和月份并将其替换为 now()