如果我没有得到结果,我想创建一个 sql 查询来检索两个日期 T1 和 T2 之间的元素,我想得到这些日期之外的结果。我怎么能只使用 sql
select * from myTable where tmp >= T1 and tmp <= T2
select * from myTable where tmp < T1 or tmp > T2
如果我没有得到结果,我想创建一个 sql 查询来检索两个日期 T1 和 T2 之间的元素,我想得到这些日期之外的结果。我怎么能只使用 sql
select * from myTable where tmp >= T1 and tmp <= T2
select * from myTable where tmp < T1 or tmp > T2
与此类似的查询应该可以工作:
(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)
考虑到目前提供的详细信息,这未经测试。
下面的查询应该可以解决这个问题:
SELECT *
FROM myTable
WHERE
(tmp BETWEEN T1 AND T2)
OR
((tmp < T1 OR tmp > T2) AND (tmp NOT BETWEEN T1 AND T2))