【问题标题】:SQLITE get row continous valueSQLITE 获取行连续值
【发布时间】:2016-02-05 18:22:45
【问题描述】:

我有一张简单的桌子。示例行之一如下所示:

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 |

只有我现在是在我输入的数字之后全选,并以编程方式查找连续。

我该怎么办?

【问题讨论】:

  • 你的问题我不清楚。
  • 对不起。我不擅长英语。我想找到最大连续数。
  • 1-2-3-100-104 -> 结果:3
  • 5-6-7-8-10-11 -> 结果:8
  • 4-6-10-11-12-15(从 10 开始)-> 结果:12

标签: sqlite select android-sqlite


【解决方案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;

【讨论】:

  • select id FROM tableName t1 WHERE t1.id >= 10 AND not exists(select id from tableName where id = t1.id + 1) order by t1.id LIMIT 1
  • @Soo 没关系,只要不要求10到输出id之间的每一个整数都存在,应该比我的好
  • 非常感谢您的重播。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-04
  • 2022-01-12
  • 1970-01-01
  • 2018-10-15
  • 2021-11-08
  • 1970-01-01
相关资源
最近更新 更多