急!从SQL语句提取字段值,请大虾帮忙!

qdubit 2004-11-23 03:25:50
我想从满足一些条件的两条记录中提取其中一条的字段值,怎么写SQL语句呀?
比如:
select top 2 xingming from table where xingbie='男' and nianling>'25' order by riqi desc
上面的语句中,我想得到第二条记录的xingming,怎么写呢?
...全文
114 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
631799 2004-11-23
  • 打赏
  • 举报
回复
print replace(right(@s,len(@s)-(charindex(',',@s)+1),',','')--少了个'('
631799 2004-11-23
  • 打赏
  • 举报
回复
declare @s varchar(100)
set @s=''
select top 2 @s=@s+xingming+',' from table where xingbie='男' and nianling>'25' order by riqi desc

print replace(right(@s,len(@s)-charindex(',',@s)+1),',','')
631799 2004-11-23
  • 打赏
  • 举报
回复
declare @s varchar(100)
set @s=''
select top 2 @s=@s+xingming+',' from table where xingbie='男' and nianling>'25' order by riqi asc--这里应该为asc.

print left(@s,charindex(',',@s)-1)
davorsuker39 2004-11-23
  • 打赏
  • 举报
回复
select top 1 xingming
from (select top 2 xingming from table1
where xingbie='男' and nianling>'25'
order by riqi desc) t order by riqi
Andy__Huang 2004-11-23
  • 打赏
  • 举报
回复
declare @s varchar(100)
set @s=''
select top 2 @s=@s+xingming+',' from table where xingbie='男' and nianling>'25' order by riqi desc

print left(@s,charindex(',',@s)-1)

zjcxc 2004-11-23
  • 打赏
  • 举报
回复
--或者:
declare @xingming varcahr(8000)
select top 2 @xingming =xingming from table where xingbie='男' and nianling>'25' order by riqi desc
select @xingming
zjcxc 2004-11-23
  • 打赏
  • 举报
回复
select top 1 xingming from(
select top 2 xingming from table where xingbie='男' and nianling>'25' order by riqi desc)a order by riqi
pbsql 2004-11-23
  • 打赏
  • 举报
回复
select top 1 xingming
from (select top 2 xingming from table1
where xingbie='男' and nianling>'25'
order by riqi desc) t order by riqi

27,579

社区成员

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

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