我需要一些帮助SQL查询.
我试图从表test_table中选择不适合两个日期“2009-12-15”和“2010-01-02”之间的所有记录.
这是我的表结构:
`start_date` date NOT NULL default '0000-00-00',`end_date` date NOT NULL default '0000-00-00' ----------------------------- **The following record should not be selected:** `start_date`,`end_date` '2003-06-04','2010-01-01'
我的查询:
SELECT * FROM `test_table` WHERE CAST('2009-12-15' AS DATE) NOT BETWEEN start_date and end_date AND CAST('2010-01-02' AS DATE) NOT BETWEEN start_date and end_date
任何想法为什么我的查询选择错误记录?我应该将查询中的值顺序更改为:
start_date NOT BETWEEN CAST('2009-12-15' AS DATE) and CAST('2010-01-02' AS DATE)
非常感谢任何帮助
解决方法
如何尝试:
select * from 'test_table' where end_date < CAST('2009-12-15' AS DATE) or start_date > CAST('2010-01-02' AS DATE)
这将返回与您的日期范围不重叠的所有日期范围.