基本上,我想在之前和之前的1个条目中选择+ 1个条目。
我试过这样:
SELECT DateTime, value FROM ReadingGlucoseBlood WHERE DateTime BETWEEN '2011-01-21 00:00:00' AND datetime('2011-01-21 00:00:00', '+24 hours')
UNION
SELECT DateTime, value FROM ReadingGlucoseBlood WHERE DateTime < '2011-01-21 00:00:00' LIMIT 1
UNION
SELECT DateTime, value FROM ReadingGlucoseBlood WHERE DateTime > datetime('2011-01-21 00:00:00', '+24 hours') LIMIT 1
ORDER BY DateTime ASC
但事实证明我在复合SELECT语句的最后只能有一个LIMIT ..所以我该如何实现这个效果呢? (使用sqlite btw)
答案 0 :(得分:2)
您需要子查询LIMIT以限制其范围
SELECT DateTime, value FROM ReadingGlucoseBlood WHERE DateTime BETWEEN '2011-01-21 00:00:00' AND datetime('2011-01-21 00:00:00', '+24 hours')
UNION ALL
SELECT * FROM (
SELECT DateTime, value FROM ReadingGlucoseBlood WHERE DateTime < '2011-01-21 00:00:00'
ORDER BY DateTime DESC LIMIT 1
) X
UNION ALL
SELECT * FROM (
SELECT DateTime, value FROM ReadingGlucoseBlood WHERE DateTime > datetime('2011-01-21 00:00:00', '+24 hours')
ORDER BY DateTime ASC LIMIT 1
) Y
ORDER BY DateTime ASC