问题 关于sql查询

bcy123456 2003-12-02 04:54:23
我要查询数据库表里mm表,查询年龄大于50,我想在查询的结果里加一个序号,如1,2,3...,
如,select name from mm where nl>50
有什么加一个序号,而且序号是顺序的
结果形式是:
1 bcy
2 ldy
3 ml
. .
. .
. .
. .
. .
...全文
92 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
dddsdfsd1 2003-12-02
  • 打赏
  • 举报
回复
create table #tem(no int identity,name varchar(20))
insert #tem select name from mm where n1>50
select * from #tem
drop table #tem
victorycyz 2003-12-02
  • 打赏
  • 举报
回复
select a.name into #A from MM a where a.nl>50
alter table #A add id int identity(1,1)
select * from #A
sdhdy 2003-12-02
  • 打赏
  • 举报
回复
select identity(int,1,1) FID,name into #temp from mm where nl>50
select * from #temp
drop table #temp
lansquenet 2003-12-02
  • 打赏
  • 举报
回复
SELECT identity(int, 1, 1) id, name INTO # FROM mm WHERE nl>50
SELECT * FROM #
sdhdy 2003-12-02
  • 打赏
  • 举报
回复
select identity(int,1,1) FID,name into #temp from from mm where nl>50
select * from #temp
drop table #temp
realgz 2003-12-02
  • 打赏
  • 举报
回复
错了
1,select * into #tmp from table
alter #tmp add cnt int identity(1,1)
select * from #tmp
2,要有一个可以唯一区分的列或列组合
select *,(select count(*) from table t2 where t2.id<=t.id) as cnt from table t
zarge 2003-12-02
  • 打赏
  • 举报
回复
select identity(int, 1, 1) as sn, name into #tmp from mm where nl>50
select * from #tmp
wzh1215 2003-12-02
  • 打赏
  • 举报
回复
如果表里有主键:
select (select count(*)+1 from mm a where a.主键>主键 and n1>50) as id,name from mm where n1>50
realgz 2003-12-02
  • 打赏
  • 举报
回复
目前主要有2种方法
1,select * into #tmp from table
alter #tmp add cnt identity(1,1)
select * from #tmp
2,有一个可以唯一区分的列或列组合
select *,(select count(*) from table t2 where t2.id<=t.id) as cnt from table t

34,874

社区成员

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

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