No Name
1 a
2 b
5 c
4 d
8 e
7 f
如何查找No列里最大值所对应的Name列的值(比如No列的最大值8,对应的Name列的值e)?
7 个解决方案
#1
select no name from 表A
where no = (select max(No) from 表A)
where no = (select max(No) from 表A)
#2
select top 1 name from a order by no desc
#3
(1)
select no ,name from 表A
where no = (select max(No) from 表A)
#4
or(如果有重复可能返回多条):
select name from a where no=(select max(no) from a)
#5
select top 1 name from a order by no desc
=============================================
TOP 1 只取出一条,有重复的就不行了
=============================================
TOP 1 只取出一条,有重复的就不行了
#6
/*No Name
1 a
2 b
5 c
4 d
8 e
7 f */
declare @t table([No] int,[Name] char(2))
insert @t select 1,'a'
union all select 2,'b'
union all select 5,'c'
union all select 4,'d'
union all select 8,'e'
union all select 7,'f'
select *
from @t
where [No]=(select max([No]) from @t)
/*
(所影响的行数为 6 行)
No Name
----------- ----
8 e
(所影响的行数为 1 行)
*/
#7
1、select no , name from 表A
where no = (select max(No) from 表A)
2.select no , name from 表A a
where not exists(select 1 from 表A b where a.no<b.no )
#1
select no name from 表A
where no = (select max(No) from 表A)
where no = (select max(No) from 表A)
#2
select top 1 name from a order by no desc
#3
(1)
select no ,name from 表A
where no = (select max(No) from 表A)
#4
or(如果有重复可能返回多条):
select name from a where no=(select max(no) from a)
#5
select top 1 name from a order by no desc
=============================================
TOP 1 只取出一条,有重复的就不行了
=============================================
TOP 1 只取出一条,有重复的就不行了
#6
/*No Name
1 a
2 b
5 c
4 d
8 e
7 f */
declare @t table([No] int,[Name] char(2))
insert @t select 1,'a'
union all select 2,'b'
union all select 5,'c'
union all select 4,'d'
union all select 8,'e'
union all select 7,'f'
select *
from @t
where [No]=(select max([No]) from @t)
/*
(所影响的行数为 6 行)
No Name
----------- ----
8 e
(所影响的行数为 1 行)
*/
#7
1、select no , name from 表A
where no = (select max(No) from 表A)
2.select no , name from 表A a
where not exists(select 1 from 表A b where a.no<b.no )