SQLite根据where子句选择下一行和上一行

时间:2010-02-02 12:44:02

标签: sql sqlite

我希望能够使用SQLite get下一行和上一行。

id  statusid  date
168 1   2010-01-28 16:42:27.167
164 1   2010-01-28 08:52:07.207
163 1   2010-01-28 08:51:20.813
161 1   2010-01-28 07:10:35.373
160 1   2010-01-27 16:09:32.550
 46 2   2010-01-30 17:13:45.750
145 2   2010-01-30 17:13:42.607
142 2   2010-01-30 16:11:58.020
140 2   2010-01-30 15:45:00.543

例如:

鉴于 id 46 ,我想返回ID 160 (上一个)和 145 (下一个)

鉴于 id 160 ,我想返回ID 161 (前一个)和 46 (下一个) 等...

请注意数据按statusId then dateCreated DESC HAS 排序,以便使用SQLite。

select * from @t order by statusId, dateCreated desc

测试在sql server中创建的数据......

set nocount on; set dateformat ymd;
declare @t table(id int, statusId int, dateCreated datetime)
insert into @t
select 168,1,'2010-01-28 16:42:27.167' union
select 164,1,'2010-01-28 08:52:07.207' union
select 163,1,'2010-01-28 08:51:20.813' union
select 161,1,'2010-01-28 07:10:35.373' union
select 160,1,'2010-01-27 16:09:32.550' union
select  46,2,'2010-01-30 17:13:45.750' union
select 145,2,'2010-01-30 17:13:42.607' union
select 142,2,'2010-01-30 16:11:58.020' union
select 140,2,'2010-01-30 15:45:00.543'

使用SQL Server 2005+这将是相当简单的!

修改

这是相同的测试数据脚本,但 SQLite 是问题的焦点。

create table t (id int, statusId int, dateCreated datetime);
insert into t
select 168,1,'2010-01-28 16:42:27.167' union
select 164,1,'2010-01-28 08:52:07.207' union
select 163,1,'2010-01-28 08:51:20.813' union
select 161,1,'2010-01-28 07:10:35.373' union
select 160,1,'2010-01-27 16:09:32.550' union
select  46,2,'2010-01-30 17:13:45.750' union
select 145,2,'2010-01-30 17:13:42.607' union
select 142,2,'2010-01-30 16:11:58.020' union
select 140,2,'2010-01-30 15:45:00.543';

编辑2 请注意,数据不是一个好例子,所以我将id 146更改为46

2 个答案:

答案 0 :(得分:3)

此问题比首次出现时要复杂得多。两个按字段排序必须单独处理,然后与联合组合并获取适当的结果。为了获得前一个和下一个,我们需要另一个联合,所以我们最终得到一个与子联合的联合。

这适用于提供的数据。我测试了很多输入,得到了正确的上一个/下一个输出。使用时,请确保获取要替换的146的所有实例。

SELECT *
FROM
(
    SELECT  t1.*
    FROM    t t1,
            (
                SELECT  *
                FROM    t
                WHERE   id = 146
            ) t2
    WHERE   t1.statusid = t2.statusid
      AND   t1.dateCreated >= t2.dateCreated
      AND   t1.id <> 146

    UNION

    SELECT  t1.*
    FROM    t t1,
            (
                SELECT  *
                FROM    t
                WHERE   id = 146
            ) t2
    WHERE   t1.statusid < t2.statusid


    ORDER BY             
            t1.statusid DESC,
            t1.dateCreated 

    LIMIT 1
)

UNION

SELECT *
FROM 
(
    SELECT  t1.*
    FROM    t t1,
            (
                SELECT  *
                FROM    t
                WHERE   id = 146
            ) t2
    WHERE   t1.statusid = t2.statusid
      AND   t1.dateCreated <= t2.dateCreated
      AND   t1.id <> 146

    UNION

    SELECT  t1.*
    FROM    t t1,
            (
                SELECT  *
                FROM    t
                WHERE   id = 146
            ) t2
    WHERE   t1.statusid > t2.statusid


    ORDER BY             
            t1.statusid,
            t1.dateCreated DESC

    LIMIT 1
)

ORDER BY             
        statusid,
        dateCreated DESC
;

答案 1 :(得分:0)

select id from theTable where id>@id order by id desc limit 1
union
select id from theTable where id<@id order by id desc limit 1