Sqllite:选择2个日期之间的记录,包括插入行

时间:2016-12-14 20:22:01

标签: sql sqlite

我在sqllite数据库表(mytable)中有一些数据:

id  column1  date
111 280 2014-02-01
112 281 invalid invalid invalid
113 282 invalid invalid invalid
114 275 2014-02-01
......................
338 273 2014-02-28

我想在2月份选择所有记录。有些干预记录无效且没有日期。我试过了:

SELECT * FROM mytable WHERE date BETWEEN '2014-02-01' AND '2014-03-01' 

但这会跳过介入的记录

我也希望选择这些,以便我最终得到id 111-338之间的所有记录

除id之外的所有列都是文本格式。

我该怎么做?

1 个答案:

答案 0 :(得分:3)

现在没有sqlite,但这应该给你一个开始:

select max(id) as max_id, min(id) as min_id
from mytable
where date BETWEEN '2014-02-01' AND '2014-03-01'

然后

select * 
from mytable where id >= min_id and id <= max_id

我认为这只会在一个查询中出现:

select mytable.* 
from (select max(id) as max_id, min(id) as min_id
      from mytable
      where date BETWEEN '2014-02-01' AND '2014-03-01'
) as internal, mytable 
where mytable.id >= internal.min_id and mytable.id <= internal.max_id