请教关于 sqlite中的查询结果赋值

SUMI_SUMI 2019-09-06 09:18:41
数据库sqlite
我有一个查询结果

select * from tableA where Type = 2 order by seq

如上
发现这个查询结果里面的seq是有重复数值的
尝试过取 rowid,但是rowid实际上取的是数据插入时的真实ID,无法实现递增效果
所以我想把结果中的seq字段从1开始重新赋值
我应该怎么做?

...全文
541 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
SUMI_SUMI 2019-09-09
  • 打赏
  • 举报
回复
引用 5 楼 独木成林_ 的回复:
[quote=引用 4 楼 SUMI_SUMI 的回复:]
[quote=引用 3 楼 独木成林_ 的回复:]
update du set seq=a.rowno from (select * from du where T_Type = 7 and ifparent =0 and PID = 0 order by seq) as a
where a.id=du.id
[/code]

上面这个语句执行不成功
实际上我发现我自己上面的语句也有问题,以前的seq有一组重复连续2个seq的值都是149
执行语句后获取的值都变成了150
[/quote]你把完整的表結構發出來。[/quote]
谢谢
查了下好像sqlite的话用代码,获取行号不行
最后用代码实现了
虽然感觉这样效率比较低
SUMI_SUMI 2019-09-06
  • 打赏
  • 举报
回复
select id,seq,(select count(distinct id) from (select * from du where T_Type = 7 and ifparent =0 and PID = 0 order by seq) a where a.seq<=b.seq) as rowno from (select * from du where T_Type = 7 and ifparent =0 and PID = 0 order by seq) b

我用上面的语句查询出了结果,入选

我应该怎么把rowno的值赋给<select * from du where T_Type = 7 and ifparent =0 and PID = 0 order by seq>这个查询结果的seq
达到更新seq排序的目的呢?
独木成林_ 2019-09-06
  • 打赏
  • 举报
回复


if not object_id(N'Tempdb..#tab') is null
	drop table #tab
Go
Create table #tab([name] nvarchar(21),[qname] nvarchar(24),[heji] int)
Insert #tab
select N'A',N'永久',10 union all
select N'A',N'12個月',20 union all
select N'A',N'6個月',30 union all
select N'A',N'3個月',40 union all
select N'A',N'永久',15 union all
select N'A',N'12個月',6 union all
select N'A',N'6個月',5 union all
select N'B',N'永久',20 union all
select N'B',N'12個月',30 union all
select N'B',N'6個月',40 union all
select N'B',N'3個月',50 union all
select N'B',N'永久',1
Go
select * from #tab
Select IDENTITY(INT,1,1) AS 序号,*  into #a from #tab
select * from #a

drop table #tab
drop table #a

独木成林_ 2019-09-06
  • 打赏
  • 举报
回复
引用 4 楼 SUMI_SUMI 的回复:
[quote=引用 3 楼 独木成林_ 的回复:] update du set seq=a.rowno from (select * from du where T_Type = 7 and ifparent =0 and PID = 0 order by seq) as a where a.id=du.id [/code]
上面这个语句执行不成功 实际上我发现我自己上面的语句也有问题,以前的seq有一组重复连续2个seq的值都是149 执行语句后获取的值都变成了150 [/quote]你把完整的表結構發出來。
SUMI_SUMI 2019-09-06
  • 打赏
  • 举报
回复
引用 3 楼 独木成林_ 的回复:
update du set seq=a.rowno from (select * from du where T_Type = 7 and ifparent =0 and PID = 0 order by seq) as a
where a.id=du.id
[/code]

上面这个语句执行不成功
实际上我发现我自己上面的语句也有问题,以前的seq有一组重复连续2个seq的值都是149
执行语句后获取的值都变成了150
独木成林_ 2019-09-06
  • 打赏
  • 举报
回复
引用 2 楼 SUMI_SUMI 的回复:
select id,seq,(select count(distinct id) from (select * from du where T_Type = 7 and ifparent =0 and PID = 0 order by seq) a where a.seq<=b.seq) as rowno from (select * from du where T_Type = 7 and ifparent =0 and PID = 0 order by seq) b
我用上面的语句查询出了结果,入选 我应该怎么把rowno的值赋给<select * from du where T_Type = 7 and ifparent =0 and PID = 0 order by seq>这个查询结果的seq 达到更新seq排序的目的呢?


update du set seq=a.rowno from (select * from du where T_Type = 7 and ifparent =0 and PID = 0 order by seq) as a
where a.id=du.id

22,299

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧