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
Count days in date 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 datediff
function:
您可以使用 MySQLdatediff
函数:
SELECT DATEDIFF('2010-01-01','2010-01-31') AS DiffDays
It should return a floating point, where 1.0
represents 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
回答by Michaelkay
回答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(*) 查询相同的查询中使用它?