关于SQL查询的一个问题

linghuqiong 2006-04-26 09:21:41
SQL用WHERE条件查询出表中的项目后,想知道其中某一项在这查出来的项目中(不是表里的所有项目)排在第几位(按照ID排序),应该怎样得到?比如从全体学生表里查出女生,然后想得到叫**的是查出的这些女生中的第几个。
...全文
201 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
simonhehe 2006-04-28
  • 打赏
  • 举报
回复
楼主,各位同胞,为什么我用下面的句子(itblog的)结果就不是想要的呢? 顺便up
select ID,[name],sex,第几位=(select count(*) from #t where [name]=a.[name] and sex=a.sex and id<a.id)+1 from #t a order by id
---------显示
id name sex 第几位
1 天仙美眉 0 1
2 芙蓉姐姐 0 1
3 姚明 1 1
4 大致 1 1
xeqtr1982 2006-04-28
  • 打赏
  • 举报
回复
declare @t table([id] int,[name] varchar(20),sex bit)
insert into @t select 1,'天仙美眉','0'
union all select 2,'芙蓉姐姐','0'
union all select 3,'姚明','1'
union all select 4,'大致','1'

select ID,[name],sex,第几位=(select count(*) from @t where sex=a.sex and id<a.id)+1 from @t a order by id

simonhehe 2006-04-27
  • 打赏
  • 举报
回复
create table #t([id] int,[name] varchar(20),sex bit)
insert into #t select 1,'天仙美眉','0'
union all select 2,'芙蓉姐姐','0'
union all select 3,'姚明','1'
union all select 4,'大致','1'
go

declare @name varchar(10)
set @name='芙蓉姐姐'
select *,次序=
(case [name] when @name then (select count(*) from #t where [id] <=(select [id] from #t where [name]=@name))else null end)
from #t where sex=0

--drop table #t 如果不用变量,在频繁使用时不方便
xeqtr1982 2006-04-27
  • 打赏
  • 举报
回复
SELECT CAST (ID as int(4))as ID,case when substring(StatusCode,2,1)='1' then '已签到' else '未签到' END AS 'StatusCode'From Meeting
linghuqiong 2006-04-27
  • 打赏
  • 举报
回复
感谢itblog(i like i do)

又出现问题了
SELECT CAST (ID as int(4))as ID,case StatusCode when substring(StatusCode,2,1)='1' then '已签到' else '未签到' END AS 'StatusCode'From Meeting

第 1 行: '=' 附近有语法错误。

就是 substring(StatusCode,2,1)='1' 这个地方的等号
应该如何改?
itblog 2006-04-26
  • 打赏
  • 举报
回复
select ID,姓名,性别,第几位=(select count(*) from 你的表名 where 姓名=a.姓名 and 性别=a.性别 and id<a.id)+1 from 你的表名 a order by id
linghuqiong 2006-04-26
  • 打赏
  • 举报
回复
表里面有这样几列(ID 姓名 性别)
按照ID排序
itblog 2006-04-26
  • 打赏
  • 举报
回复
表结构呢?

27,581

社区成员

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

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