用于获取过去 3 个月数据的 SQL 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21912173/
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
SQL query for getting data for last 3 months
提问by user88866050
How can you get today's date and convert it to 01/mm /yyyy
format and get data from the table with delivery month 3 months ago? Table already contains delivery month as 01/mm/yyyy
.
您如何获取今天的日期并将其转换为01/mm /yyyy
格式并从 3 个月前交付月份的表中获取数据?表中已包含交货月份为01/mm/yyyy
。
回答by M.Ali
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(MONTH, -3, GETDATE())
Mureinik's suggested method will return the same results, but doing it this way your query can benefit from any indexes on Date_Column
.
Mureinik 建议的方法将返回相同的结果,但通过这种方式,您的查询可以从Date_Column
.
or you can check against last 90 days.
或者您可以检查过去 90 天。
SELECT *
FROM TABLE_NAME
WHERE Date_Column >= DATEADD(DAY, -90, GETDATE())
回答by Mithun Arunan
Latest Versions of mysql don't support DATEADD instead use the syntax
最新版本的 mysql 不支持 DATEADD 而是使用语法
DATE_ADD(date,INTERVAL expr type)
To get the last 3 months data use,
要获得最近 3 个月的数据使用情况,
DATE_ADD(NOW(),INTERVAL -90 DAY)
DATE_ADD(NOW(), INTERVAL -3 MONTH)
回答by Mureinik
I'd use datediff
, and not care about format conversions:
我会使用datediff
,而不关心格式转换:
SELECT *
FROM mytable
WHERE DATEDIFF(MONTH, my_date_column, GETDATE()) <= 3
回答by Tom McDonough
Last 3 months
最近 3 个月
SELECT DATEADD(dd,DATEDIFF(dd,0,DATEADD(mm,-3,GETDATE())),0)
Today
今天
SELECT DATEADD(dd,DATEDIFF(dd,0,GETDATE()),0)