MySQL查询问题

时间:2010-09-17 06:11:55

标签: sql mysql

我有一张桌子试图过滤特定日期之间的特定ID(摆脱)......但是!我得到的是一个不将ID(rid)视为条件的查询。

这是查询:

  SELECT * FROM booking 
  WHERE '2010-10-01' BETWEEN datefrom AND dateto - INTERVAL 1 DAY 
    OR '2010-10-09' BETWEEN datefrom + INTERVAL 1 DAY AND dateto 
    OR datefrom BETWEEN '2010-10-01' AND '2010-10-09' - INTERVAL 1 DAY 
   AND **rid = '5' 
   AND active = '1'** 
   LIMIT 0 , 30

这是预订的表格结构:

bid     gid      rid      datefrom        dateto          active
=================================================================
1       1        1        2010-09-16      2010-09-20      1
8       9        2        2010-09-06      2010-09-16      1
7       8        2        2010-09-23      2010-09-28      1

3 个答案:

答案 0 :(得分:3)

尝试使用括号表示日期条件:

  SELECT * FROM booking 
  WHERE rid = '5' 
   AND active = '1'
   AND ('2010-10-01' BETWEEN datefrom AND dateto - INTERVAL 1 DAY 
    OR '2010-10-09' BETWEEN datefrom + INTERVAL 1 DAY AND dateto 
    OR datefrom BETWEEN '2010-10-01' AND '2010-10-09' - INTERVAL 1 DAY) 
   LIMIT 0 , 30

答案 1 :(得分:0)

尝试这个

SELECT * FROM booking WHERE
('2010-10-01' BETWEEN datefrom AND dateto - INTERVAL 1 DAY) 
OR ('2010-10-09' BETWEEN datefrom + INTERVAL 1 DAY AND dateto) 
OR (datefrom BETWEEN '2010-10-01' AND '2010-10-09' - INTERVAL 1 DAY) 
AND rid = '5' AND active = '1' 
LIMIT 0 , 30

答案 2 :(得分:0)

我认为您需要在所有OR条件中使用一些括号作为一个组,以便它知道如何在最后应用AND条件。