Select 查询问题

zhanghuacheng 2008-04-23 09:38:09
如果要用select语句对一个表进行普通查询,可以让查询的结果中加入一个递增数列吗?如:
1 aaa1 bbb1 ccc1
2 aaa2 bbb2 ccc2
3 aaa3 bbb3 ccc3
4 aaa4 bbb4 ccc4
.....

其中,aaa、bbb、ccc为表中字段,而:1、2、3、4.....是人为添加的递增数列
...全文
148 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
TNT_1st_excellence 2008-04-25
  • 打赏
  • 举报
回复
study
hui_hui_2007 2008-04-24
  • 打赏
  • 举报
回复
如果有惟一列的话,就可以用下面的方法,这样不用生成临时表

use northwind
select id = (select count(1) from Customers where CustomerID < t.CustomerID) + 1 ,* from Customers t
hui_hui_2007 2008-04-24
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dawugui 的回复:]
--方法一
select id = identity(int,1,1) , * into #tmp from tb
select * from tb
[/Quote]

改一下:
select id = identity(int,1,1) , * into #tmp from tb
select * from #tmp
dawugui 2008-04-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 zhanghuacheng 的帖子:]
如果要用select语句对一个表进行普通查询,可以让查询的结果中加入一个递增数列吗?如:
1 aaa1 bbb1 ccc1
2 aaa2 bbb2 ccc2
3 aaa3 bbb3 ccc3
4 aaa4 bbb4 ccc4
.....

其中,aaa、bbb、ccc为表中字段,而:1、2、3、4.....是人为添加的递增数列
[/Quote]
--方法一
select id = identity(int,1,1) , * into tmp from tb
select * from tb

--方法二
select id = (select count(1) from tb where aaa < t.aaa) + 1 from tb t

--方法三
select id = (select count(1) from tb where aaa < t.aaa or (aaa = t.aaa and bbb < t.bbb) or (aaa = t.aaa and bbb = t.bbb and ccc < t.ccc)) + 1 from tb t

--方法四(sql server 2005)
select id = row_number() over(order by aaa , bbb, ccc) , * from tb
真汉子 2008-04-23
  • 打赏
  • 举报
回复

不知用 select 字段名.... into #t from tb 能不能实现楼主想要的结果
areswang 2008-04-23
  • 打赏
  • 举报
回复
可以通过
select id=identity(int),col1,col2 into #tb
Limpire 2008-04-23
  • 打赏
  • 举报
回复
select id=identity(int,1,1) into # from 表
select * from #
-狙击手- 2008-04-23
  • 打赏
  • 举报
回复
select id = (select count(1) from ta where col< a.col),
*
from ta a
--col唯一标识列
-狙击手- 2008-04-23
  • 打赏
  • 举报
回复
1 select id = identity(int,1,1),* into # from ta
select * from #

34,594

社区成员

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

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