SQL - 获取昨天和前一天的数据

时间:2013-08-28 18:07:59

标签: sql

我正在运行此查询:

select member, customerinfo.customerid -- ...other irrelevant columns...
from customerinfo, addressinfo
    where customerinfo.customerid = addressinfo.customerid
    and MEMBER = (Date(GetDate()-1))
    and addressinfo.addresstype = 's'

如果会员=昨天,我显然会给我数据。

我的问题是,如果会员=过去2天(昨天和前一天),如何构建查询以向我提供数据?

2 个答案:

答案 0 :(得分:9)

MEMBER BETWEEN (GETDATE() -2) AND (GETDATE() -1)

在SQL Server中,您还可以尝试:

MEMBER BETWEEN DATEADD(day, -2, GETDATE()) AND DATEADD(day, -1, GETDATE())

答案 1 :(得分:1)

更改您的查询:

SELECT member, customerinfo.customerid, ContactName, Address1,
Address2, City, State, ZIP, Country from customerinfo, addressinfo
WHERE customerinfo.customerid = addressinfo.customerid
and MEMBER >= (Date(GetDate()-2)) AND MEMBER <= (Date(GetDate()-1))
and addressinfo.addresstype = 's'