MySQL多个条件,结果不符合所有条件

问题描述 投票:0回答:1

我有一个从expressJS注入MySQL的查询,但是当执行这些查询时。有时我返回的数据不符合条件。


原版的:

SELECT * FROM table
  WHERE status = 'Invoiced'
  AND (ordereddate between '2009-12-01' AND '2011-12-30')
  OR (duedate between '2009-12-01' AND '2011-12-30');

结果:

Ordered Date   Due Date      Status
2009-11-18     2009-12-16    Invoiced
2009-11-18     2009-12-14    Cancelled

结果显示正在提取具有任何状态的项目,但会满足日期条件。


尝试:

SELECT * FROM table
  WHERE (status = 'Invoiced')
  AND (ordereddate between '2009-12-01' AND '2011-12-30')
  AND (duedate between '2009-12-01' AND '2011-12-30');

结果:如果ordereddateduedate超出日期范围,则该项目将被排除在结果之外。

改为AND而不是OR


尝试:

SELECT * FROM table
  WHERE status = 'Invoiced'
  AND (ordereddate AND duedate between '2009-12-01' AND '2011-12-30');

结果:正确返回所有内容,除非ordereddateduedate超出日期范围,否则该行将被排除在结果之外。

结合ordereddateduedate的情况


尝试:

SELECT * FROM table
  WHERE status = 'Invoiced'
  AND (ordereddate OR duedate between '2009-12-01' AND '2011-12-30');

结果:

Ordered Date   Due Date      Status
2008-07-07     2008-07-28    Invoiced
2008-07-07     2008-07-25    Invoiced
2008-07-07     2008-07-23    Invoiced

这个似乎拉出正确的状态,但是日期不正确,它似乎是拉动状态已开票的任何东西,并忽略日期范围。

mysql node.js mariadb where between
1个回答
0
投票

如果我正确理解您的问题,您需要使用其他括号:

SELECT
    *
FROM
    table
WHERE
    status = 'Invoiced'
    AND (
           (ordereddate between '2009-12-01' AND '2011-12-30')
        OR (duedate between '2009-12-01' AND '2011-12-30')
    )
;
© www.soinside.com 2019 - 2024. All rights reserved.