一个sql的问题,怎么在查询中增加一个自增列(给出结果立刻结帖)

jetdw 2007-08-06 10:12:43
比如查询语句select stor_id,stor_name,stor_address from pubs..stores的结果中为

stor_id stor_name stor_address
6380 Eric the Read Books 788 Catamaugus Ave.
7066 Barnum's 567 Pasadena Ave.
7067 News & Brews 577 First St.
7131 Doc-U-Mat: Quality Laundry and Books 24-A Avogadro Way
7896 Fricative Bookshop 89 Madison St.
8042 Bookbeat 679 Carson St.

我想在结果中增加一列,形式为:
id stor_id stor_name stor_address
1 6380 Eric the Read Books 788 Catamaugus Ave.
2 7066 Barnum's 567 Pasadena Ave.
3 7067 News & Brews 577 First St.
4 7131 Doc-U-Mat: Quality Laundry and Books 24-A Avogadro Way
5 7896 Fricative Bookshop 89 Madison St.
6 8042 Bookbeat 679 Carson St.

请大家帮下忙,环境是mssql2000

...全文
1061 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
cxmcxm 2007-08-08
  • 打赏
  • 举报
回复
关系数据库,本来就是靠关系,不靠顺序,
硬来即使写出来,效率也会奇低.
  • 打赏
  • 举报
回复
如果是2005,可以使用ROW_NUMBER(),2000好象只能借助临时表!
jetdw 2007-08-08
  • 打赏
  • 举报
回复
老大们来解决下呀
Limpire 2007-08-08
  • 打赏
  • 举报
回复
楼上正解。

套用一下:

关系社会,本来就是靠关系,不靠能力,
硬来即使串上去,成活率也会奇低。
yinsedeshi 2007-08-07
  • 打赏
  • 举报
回复
select stor_id,stor_name,stor_address,rank()over(partion by stor_id)as rank from pubs.stores
yinsedeshi 2007-08-07
  • 打赏
  • 举报
回复
可以用rank()函数吗?
就是
select stor_id,stor_name,stor_address,rank()over(partion by stor_id)as rank
jetdw 2007-08-07
  • 打赏
  • 举报
回复
请各位高手们看看呀
jetdw 2007-08-06
  • 打赏
  • 举报
回复
请大家处理下,麻烦大家了
jetdw 2007-08-06
  • 打赏
  • 举报
回复
没有,只是查出来的记录后增加的,字段不统一,也不能确定哪个是主键
mugua604 2007-08-06
  • 打赏
  • 举报
回复
多加个条件不能解决?
--try
select id=(select count(*)+1 from stores where col=a.col and stor_id<a.stor_id),* from stores a
在路上_- 2007-08-06
  • 打赏
  • 举报
回复
难道没有主键?
jetdw 2007-08-06
  • 打赏
  • 举报
回复
lxzm1001(*~悠悠蓝星梦~*)
这种方式不行,上面只是举的一个例子
而实际情况可能两个或者3个字段确定一条记录
而不只是一个字段确定一条记录
麻烦大家再想想,谢谢!
nolast02 2007-08-06
  • 打赏
  • 举报
回复
就是效率低了点。。。
在路上_- 2007-08-06
  • 打赏
  • 举报
回复
高手解决了
在路上_- 2007-08-06
  • 打赏
  • 举报
回复
用两句吧,一句真的不会。
高手现身
lxzm1001 2007-08-06
  • 打赏
  • 举报
回复
select id=(select count(*)+1 from stores where stor_id<a.stor_id),* from stores a
nolast02 2007-08-06
  • 打赏
  • 举报
回复
关注,帮顶
在路上_- 2007-08-06
  • 打赏
  • 举报
回复
sorry, 错了
在路上_- 2007-08-06
  • 打赏
  • 举报
回复
select * from (select id=identity(1,1),stor_id,stor_name,stor_address into #t from pubs.stores) t
在路上_- 2007-08-06
  • 打赏
  • 举报
回复
select id=identity(1,1),stor_id,stor_name,stor_address from pubs.stores
加载更多回复(3)

34,593

社区成员

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

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