我需要有关SQL查询的帮助。
我正在尝试从表中选择所有记录,这些记录test_table将不能在两个日期“ 2009-12-15”和“ 2010-01-02”之间容纳。
test_table
这是我的表结构:
`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)
它将返回所有与您的日期范围完全不重叠的日期范围。