怎样给筛选出来的记录加"序号"?

LiveIsLive 2003-10-17 03:06:09
用Select语句筛选出来的结果,想加上一列按筛选结果顺序叠加一的一列,请问应该怎样做?

例如晒选结果是

a
t
z

我想加上一列“序号”变成

a 1
t 2
z 3
...全文
435 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qdubit 2003-10-17
  • 打赏
  • 举报
回复
select *,IDENTITY(int,1,1) 序号 into #temp from 表 where ………………
select * from #temp
drop table #temp
LiveIsLive 2003-10-17
  • 打赏
  • 举报
回复
如果改了 order by 子句,你门以上的都不行了啊。
LiveIsLive 2003-10-17
  • 打赏
  • 举报
回复
to: zjcxc(邹建)

这个语句是什么意思?我看不明白,可以解释一下吗?

select a,id=(select sum(1) from tb where a<=a.a) from tb a
order by id
txlicenhe 2003-10-17
  • 打赏
  • 举报
回复
1:
select *,IDENTITY(int,1,1) 序号 into #temp from 表 where ………………
select * from #temp
drop table #temp
2:
如果有一个字段做为主关键字,比如 a
Select *,(select sum(1) from 表 where a<= tmp.a) as 序号 from 表 tmp

sdhdy 2003-10-17
  • 打赏
  • 举报
回复
--表里没有自增字段,才可以如下
select identity(int,1,1) FID,* into #temp from tablename
go
select * from #temp
go
drop table #temp
sdhdy 2003-10-17
  • 打赏
  • 举报
回复
--表里没有自增字段,才可以如下
select identity(int,1,1) FID,* into #temp from tablename
go
select * from #temp where FID between 100 and 200
go
drop table #temp
zjcxc 元老 2003-10-17
  • 打赏
  • 举报
回复
--例子
declare @tb table(a varchar(1))
insert into @tb
select 'a'
union all select 't'
union all select 'z'

select a,id=(select sum(1) from @tb where a<=a.a) from @tb a
order by id

/*--测试结果
a id
---- -----------
a 1
t 2
z 3

(所影响的行数为 3 行)
--*/
zjcxc 元老 2003-10-17
  • 打赏
  • 举报
回复
--如果直接按顺序得到序号,可以用:
select a,id=(select sum(1) from tb where a<=a.a) from tb a
order by id
zjcxc 元老 2003-10-17
  • 打赏
  • 举报
回复
--用临时表+标识列

select *,id=identity(int,1,1) into #tb from(你的查询语句) a

--显示结果
select * from #tb

--删除临时表
shuiniu 2003-10-17
  • 打赏
  • 举报
回复
select *,IDENTITY(int,1,1) xh into #table from yourtable
select * from #table
drop table #table
yujohny 2003-10-17
  • 打赏
  • 举报
回复
往往经常有这样的需求,我需要在查询的结果中添加一列类似于Identity的数字,虽然在Client编程中并不难实现,但是有时我想留用现有的Class,不希望在Client side做额外的coding,那么就只有在Sql里面想办法了
首先介绍一种用一条SQL语句完成的办法,原理是在结果中查询大于等于该纪录的纪录条数,就可以得到它的Rank了
Example:
USE pubs
SELECT COUNT(*) AS Rank, a1.au_lname, a1.au_fname
FROM authors a1, authors a2
WHERE a1.au_lname + a1.au_fname >= a2.au_lname + a2.au_fname
GROUP BY a1.au_lname, a1.au_fname
ORDER BY Rank

select (select isnull(sum(1),0)+1 from authors where au_lname + au_fname < aa.au_lname + aa.au_fname) rank,au_lname, au_fname
from authors aa
order by rank


不过呢,这种方法有它的局限性,第一是性能不好,第二是如果存在相同的纪录,那么Rank就会出现并列的情况,比如出现两个2,但是没有3了
有没有别的方法呢?当然有的,SQL提供了一个IDENTITY Function,可以得到标识列的值,不过可惜的很的是,这个函数只能用于SELECT INTO语句,所以我们只好引入一个临时表了
Example:
USE pubs
SELECT IDENTITY(INT, 1, 1) AS Rank,au_lname,au_fname
INTO #tmp
FROM authors
SELECT * FROM #tmp
DROP TABLE #tmp
这种方法的性能和适用性都比第一种方法要强,不过缺点是必须通过几条SQL语句才能完成。
所以如果可能的话,一般还是建议在客户端完成这一操作。


yujohny 2003-10-17
  • 打赏
  • 举报
回复
select *,IDENTITY(int,1,1) 序号 into #temp from 表 where ………………
select * from #temp
drop table #temp

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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