--如果你的姓名不重复,而且按姓名排序的话,可以直接用:select id=(select sum(1) from 表A where 姓名<=a.姓名),* from 表A a order by 姓名
--查询部分select id=(select sum(1) from 表A where 姓名<=a.姓名 and age>13),* from 表A a where age>13 order by 姓名
--下面是测试--测试数据 declare @t table(Name varchar(10),Age int,Sex varchar(2)) insert into @t select '张三',10,'男' union all select '李四',12,'男' union all select '王五',14,'女' union all select '老六',45,'男'--查询全部 select id=(select sum(1) from @t where name<=a.name),* from @t a order by name--查询部分 select id=(select sum(1) from @t where name<=a.name and age>12),* from @t a where age>12 order by name/*--测试结果id Name Age Sex ----------- ---------- ----------- ---- 1 老六 45 男 2 李四 12 男 3 王五 14 女 4 张三 10 男(所影响的行数为 4 行) id Name Age Sex ----------- ---------- ----------- ---- 1 老六 45 男 2 王五 14 女(所影响的行数为 2 行) --*/
from 表A a order by 姓名
from 表A a
where age>13
order by 姓名
declare @t table(Name varchar(10),Age int,Sex varchar(2))
insert into @t
select '张三',10,'男'
union all select '李四',12,'男'
union all select '王五',14,'女'
union all select '老六',45,'男'--查询全部
select id=(select sum(1) from @t where name<=a.name),*
from @t a order by name--查询部分
select id=(select sum(1) from @t where name<=a.name and age>12),*
from @t a
where age>12
order by name/*--测试结果id Name Age Sex
----------- ---------- ----------- ----
1 老六 45 男
2 李四 12 男
3 王五 14 女
4 张三 10 男(所影响的行数为 4 行)
id Name Age Sex
----------- ---------- ----------- ----
1 老六 45 男
2 王五 14 女(所影响的行数为 2 行)
--*/