MySQL 计算日期范围内的天数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2602130/
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 15:44:46  来源:igfitidea点击:

Count days in date range?

sqlmysqldate-range

提问by JD Isaacks

I have a query like this:

我有一个这样的查询:

SELECT COUNT(*) AS amount
FROM daily_individual_tracking
WHERE sales = 'YES'
AND daily_individual_tracking_date BETWEEN '2010-01-01' AND '2010-03-31'

I am selected from a date range. Is there a way to also get the total days in the date range?

我是从一个日期范围中挑选出来的。有没有办法获得日期范围内的总天数?

回答by Unreason

Not really clear if you are looking for

不是很清楚你是否正在寻找

DATEDIFF('2010-03-31', '2010-01-01')

or

或者

COUNT(DISTINCT daily_individual_racking_date)

回答by Andomar

You can use the MySQL datedifffunction:

您可以使用 MySQLdatediff函数:

SELECT DATEDIFF('2010-01-01','2010-01-31') AS DiffDays

It should return a floating point, where 1.0represents a single day.

它应该返回一个浮点数,1.0代表一天。

And for MS SQL use ,

对于 MS SQL 使用,

SELECT DATEDIFF( day ,'2010-01-01','2010-01-31') AS DiffDays

回答by Henri

This depends on what SQL server you're using.

这取决于您使用的 SQL 服务器。

If you're using MS-SQL Server, you can use the function DateDiff

如果您使用的是 MS-SQL Server,则可以使用函数DateDiff

回答by Michaelkay

I'm not sure which SQL you are using. TSQL has a DATEDIFF that will count the number of days between two dates. See this

我不确定您使用的是哪种 SQL。TSQL 有一个 DATEDIFF 将计算两个日期之间的天数。看到这个

回答by brydgesk

What exactly are you trying to count? The total number of distinct values of daily_individual_tracking_date? Do you need it in the same query as the count(*) query?

你到底想数什么?Daily_individual_tracking_date 的不同值的总数?您是否需要在与 count(*) 查询相同的查询中使用它?