当我运行下面的查询时,它返回Date_add (CURDATE() AND CURDATE(), interval 30 day)范围内日期的所有结果,但不包括Date_sub (CURDATE() AND CURDATE(), interval 15 day)的结果
我知道当我用deadline = '2015-01-15'的精确子句查询数据时
怎么了?

SELECT bug_id,
       bug_status,
       resolution,
       short_desc,
       deadline
FROM   bugs
WHERE  bug_status IN ( 'RESOLVED' )
       AND deadline BETWEEN Curdate() AND Date_add(Curdate(), interval 30 day)
        OR deadline BETWEEN Curdate() AND Date_sub(Curdate(), interval 15 day)

最佳答案

已订购between的rand。因此,第二个between不正确。此外,您可能需要括号:

WHERE  bug_status IN ( 'RESOLVED' ) AND
       (deadline BETWEEN Curdate() AND Date_add(Curdate(), interval 30 day) OR
        deadline BETWEEN Date_sub(Curdate(), interval 15 day)  AND Curdate()
       )

我的意思是,你可能不需要括号,所以问题是:
WHERE  (bug_status IN ( 'RESOLVED' ) AND
        deadline BETWEEN Curdate() AND Date_add(Curdate(), interval 30 day)
       ) OR
       deadline BETWEEN Date_sub(Curdate(), interval 15 day)  AND Curdate()

在这种情况下,括号是多余的,但它们澄清了逻辑。

关于mysql - MySQL日期查询不正确的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28134208/

10-12 03:08