2015-11-05 58 views
0

我有一个简单的表。其中一个示例行如下所示:SQLITE获取行连续值

id | name | 
1 | a | 
2 | a | 
3 | a | 
4 | b | 
6 | b | 
7 | a | 
8 | a | 

我想获取上一个连续的id。 所以如果我开始在“1”,结果应该是“4”

在这个例子中

,结果应该是“7”

3 |a | 
4 |b | 
5 |a | 
6 |a | 
7 |a | 
10|a | 

只有我,现在是我的输入数字后选择所有,并以编程方式查找。

我该怎么办..?

+0

你的问题不清楚对我来说。 –

+0

对不起。我不擅长英语。 我想找到最大连续数。 – Soo

+0

1-2-3-100-104 - >结果:3 – Soo

回答

1

如果我理解正确的问题,这应该为你工作:

select id from tableName t1 where not exists(select id from tableName t2 where t2.id=t1.id+1) and (id-(select count(*) from tableName t3 where t3.id<t1.id))=(select min(id) from tableName);

如果你想从10开始,它应该是:

select id from tableName t1 where not exists(select id from tableName t2 where t2.id=t1.id+1) and (id-(select count(*) from tableName t3 where t3.id<t1.id and t3.id>=10))=10;

+0

是的。这正是我想要的。谢谢! – Soo

+0

select id FROM tableName t1 WHERE t1.id> = 10并且不存在(从tableName中选择id,其中id = t1.id + 1)order by t1.id LIMIT 1 – Soo

+0

这个怎么样?我有这么多的数据。 – Soo