开发者

BETWEEN and OR Select

How I can do to filter in to date range

Select * from  XXX where date between DATE1 AND DATE2 OR Between DATE3 AND DATE4

(Copied from update posted as ans开发者_Go百科wer)

Here is my condition

WHERE  ( items_count != '0' )
       AND ( main_table.is_active = '1' )
       AND ( main_table.store_id IN ( '0', '1' ) )
       AND ( main_table.updated_at BETWEEN
             '2011-03-04 16:52:19' AND '2011-03-05 16:52:19'
           )
        OR ( main_table.updated_at BETWEEN
             '2011-03-13 16:52:19' AND '2011-03-14 16:52:19'
           )
LIMIT  0, 30  

The first condition is never used


Select * from  XXX 
  where (date between DATE1 AND DATE2) 
        OR 
        (date between DATE3 AND DATE4)

EDIT:

Try this:

WHERE items_count != '0' AND 
      main_table.is_active = '1' AND 
      main_table.store_id IN ('0', '1') AND 
      (  main_table.updated_at BETWEEN '2011-03-04 16:52:19' AND '2011-03-05 16:52:19' 
         OR 
         main_table.updated_at BETWEEN '2011-03-13 16:52:19' AND '2011-03-14 16:52:19'
      )


Treat them as independent conditions:

select * from xxx where (date between date1 and date2) or (date between date3 and date4);


Like this:

 select * from XXX 
 where (date between DATE1 AND DATE2) 
    or (date between DATE3 AND DATE4)


A bit of OCD always helps with SQL...

You don't need all the brackets you have, but the OR isn't working the way you expect.

.... 
   WHERE 
    items_count != '0'        
    AND 
    main_table.is_active = '1'        
    AND 
    main_table.store_id IN ('0', '1')
    AND (
      main_table.updated_at
      BETWEEN '2011-03-04 16:52:19'
      AND '2011-03-05 16:52:19'
      OR 
      main_table.updated_at
      BETWEEN '2011-03-13 16:52:19'
      AND '2011-03-14 16:52:19'
    )
    LIMIT 0 , 30
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜