1

我正在尝试在数据库中的两个特定日期之间进行选择连接

我觉得这是在正确的道路上,但不正确

我怎样才能让它工作

SELECT --RTRIM(C.CustomerFirstName) + ' ' + LTRIM(C.CustomerLastName) as CustomerFullName,
       ROW_NUMBER() OVER(ORDER BY CP.ActionDate) AS RowNumber,
       C.CustomerFirstName,
       C.CustomerLastName,
       C.CustomerCompany,
       C.CustomerPosition,
       C.CustomerCountry,
       C.CustomerProvince,
       C.CustomerContact,
       CP.ActionDate,
       CP.ProductCode,
       CP.CustomerEmail
FROM   tblCustomers C
       JOIN tblCustomerProducts CP
            ON  C.CustomerEmail = CP.CustomerEmail


     ORDER BY ActionDate DESC
WHERE CP.ActionDate BETWEEN '1/17/2013' AND '19/12/2012'
4

4 回答 4

8

代替

WHERE CP.ActionDate BETWEEN '1/17/2013' AND '19/12/2012`

试试这个:

WHERE CP.ActionDate BETWEEN '19/12/2012' AND '1/17/2013'

注意:这是因为BETWEENSQL Server 中的谓词是非对称的,这意味着value1 BETWEEN value2 AND value3与 相同Value1 >= Value2 AND Value1 <= Value3,所以value2之前的值AND必须小于或等于value3

于 2013-01-17T11:37:19.233 回答
4

必须先列出较小的日期

BETWEEN '12/19/2012'  AND '1/17/2013'
于 2013-01-17T11:37:08.313 回答
3

您当前的查询存在一些问题。

首先,您的日期顺序错误。

其次,你ORDER BY放错地方了。这ORDER BY是您选择中列出的最后一项:

FROM tblCustomers C
JOIN tblCustomerProducts CP 
   ON C.CustomerEmail = CP.CustomerEmail
WHERE CP.ActionDate BETWEEN  '12/19/2012' AND '1/17/2013'
ORDER BY ActionDate DESC

您还可以使用:

FROM tblCustomers C
JOIN tblCustomerProducts CP 
   ON C.CustomerEmail = CP.CustomerEmail
WHERE CP.ActionDate >= '12/19/2012' AND CP.ActionDate <= '1/17/2013'
ORDER BY ActionDate DESC

我还建议您确保日期的格式相同。你有一个作为DD/MM/YYYY,另一个作为MM/DD/YYYY

于 2013-01-17T11:38:09.857 回答
2

你放错了日期,它应该在第一名较小,在第二名较大:

WHERE CP.ActionDate BETWEEN '19/12/2012' AND '1/17/2013'

或者

WHERE CP.ActionDate >= '19/12/2012' AND CP.ActionDate <= '1/17/2013'
于 2013-01-17T11:38:16.610 回答