超难的SQL语句问题

NIEC 2004-11-19 08:50:26
表如下
————————————
编号 姓名
001 张三
003 李四
002 王五
004 王二麻子
006 印度阿三
————————————
现在要求按编号排序后只取其中的一条记录,比例说排序后要取第二条记录,应该是《002 李四》这条,这样的SQL语句如何写?
...全文
106 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2004-11-19
  • 打赏
  • 举报
回复
select * from 表 a
where(select count(*) from 表 where 编号<=a.编号)=2 --取第二条记录
davorsuker39 2004-11-19
  • 打赏
  • 举报
回复
select right('000'+(select count(*) from t where 编号<=a.编号),3) as 编号,
姓名
from t a
where (select count(*) from t where 编号<=a.编号)=2
chinaandys 2004-11-19
  • 打赏
  • 举报
回复
楼主是要排序后,固定编号列吧,其实是可以在前台做的.如

select identity(int,1,1) as 编号, 姓名 into #t from 表 order by 编号
select right('0000'+cast(编号 as varchar),3) as 编号,姓名 from #t
drop table #t
lsxaa 2004-11-19
  • 打赏
  • 举报
回复
select right('000'+(select count(*) from t where 编号<=a.编号),3) as 编号,
姓名
from t a
where (select count(*) from t where 编号<=a.编号)=2
mgsray 2004-11-19
  • 打赏
  • 举报
回复
select identity(int,1,1) as id , 编号, 姓名 into #t from 表
select 编号, 姓名 from #t where id =2
drop table #t

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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