创建一个SQL查询以检索2个日期T1和T2之间或T1和T2之外的元素

时间:2013-10-04 08:51:04

标签: sql

我想创建一个sql查询来检索2个日期T1和T2之间的元素 如果我没有得到结果,我想在这些日期之外得到结果。 我怎么能只使用sql

select * from myTable where tmp >= T1 and tmp <= T2
select * from myTable where tmp < T1 or tmp > T2

2 个答案:

答案 0 :(得分:0)

与此类似的查询应该有效:

(select * from myTable where tmp >= T1 and tmp <= T2)
UNION 
(select * from myTable WHERE NOT EXISTS (select 1 from myTable where tmp >= T1 and tmp <= T2) AND tmp < T1 or tmp > T2)

考虑到目前为止提供的详细信息,未经测试。

答案 1 :(得分:0)

以下查询应该可以解决问题:

SELECT * 
FROM myTable 
WHERE
(tmp BETWEEN T1 AND T2)
OR
((tmp < T1 OR tmp > T2) AND (tmp NOT BETWEEN T1 AND T2))