设为首页 加入收藏

TOP

sqlite产生正确row_number的方法
2017-11-24 10:48:50 】 浏览:179
Tags:sqlite 产生 正确 row_number 方法

sqlite 没有 row_number , 但是有 rowid , 不过 rowid 这玩意只能在未删除过的情况是连续的,确实很坑。

下面的做法可以产生正确的行号:

drop table if exists testTable1;
create table testTable1( id INT PRIMARY KEY,[name] NVARCHAR(20), parentId INT );
INSERT INTO testTable1(id,[name],parentId) VALUES(2,'xf1',0);
INSERT INTO testTable1(id,[name],parentId) VALUES(3,'xf2',0);
INSERT INTO testTable1(id,[name],parentId) VALUES(5,'xf3',2);
INSERT INTO testTable1(id,[name],parentId) VALUES(6,'xf4',3);
INSERT INTO testTable1(id,[name],parentId) VALUES(7,'xf5',4);
INSERT INTO testTable1(id,[name],parentId) VALUES(9,'xf6',5);

delete from testTable1 where id=7;

select 
  rowid
  ,(select count(*) from testTable1 b  where a.id >= b.id) as row_number
  ,* 
from testTable1 as a 
order by id;
/*
rowid	row_number	id	name	parentId
1	        1	    2	xf1	    0
2	        2	    3	xf2	    0
3	        3	    5	xf3	    2
4	        4	    6	xf4	    3
6	        5	    9	xf6	    5
*/
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇MySQL数据库的监控性能指标、Orac.. 下一篇找回MYSQL ROOT用户密码的方法

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目