Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从SQLite中的表中获取上一个日期和小时_Sqlite - Fatal编程技术网

从SQLite中的表中获取上一个日期和小时

从SQLite中的表中获取上一个日期和小时,sqlite,Sqlite,我有以下情况: 在表中,我有4列: CommentsTBL: ID int Data datetime Hour Varchar Comment Varchar 小时列是varchar类型,不要问我为什么 给定表中的特定日期和小时,我需要查找上一条和下一条记录。如果我在前面或后面找到其中一个,那么使用相同的逻辑得到另一个就很简单了 例如: ID Data Hour Comment 1. 2011-01-12 17:00 SomeComments1 2. 2011

我有以下情况:

在表中,我有4列:

CommentsTBL:
ID int
Data datetime
Hour Varchar
Comment Varchar
小时列是varchar类型,不要问我为什么

给定表中的特定日期和小时,我需要查找上一条和下一条记录。如果我在前面或后面找到其中一个,那么使用相同的逻辑得到另一个就很简单了

例如:

ID  Data        Hour    Comment
1.  2011-01-12  17:00   SomeComments1
2.  2011-01-14  10:00   SomeComments3
3.  2011-01-14  11:00   SomeComments4
4.  2011-01-13  14:00   SomeComments2
ID 4的上一个日期为ID 1,下一个日期为ID 2

我想出了一个解决方案,但我想看看你们中是否有人有更好的,也许只使用一个选择。这是前一个:

SELECT 
       Data,
       MAX(Hour) Hour
FROM 
       CommentsTBL
WHERE 
       Data in (SELECT 
                      MAX(Data) 
                FROM 
                      CommentsTBL
                WHERE 
                      Data < MyDate or (Data = MyDate and Hour < MyHour)
               )
GROUP BY
         Data
谢谢。

为什么不:

select *
  from ( SELECT data, hour from commentsTbl
          where data < mydate
             or ( data = mydate and hour < myhour )
          order by data desc, hour desc
          limit 1 
       )
 union all
select *
  from ( SELECT data, hour from commentsTbl
          where data > mydate
             or ( data = mydate and hour > myhour )
          order by data asc, hour asc
          limit 1
       )

杰出的我不知道SQLite中有限制选项。谢谢