最大值的所有纪录,怎样写这个sql语句?
我用select max(a) from table1只返回一个最大值,可
我要的是整个纪录(如果有几个并列最大时反回几个纪录) ,不知道该怎么实现。
谢谢各位大虾的帮忙!!
12 个解决方案
#1
select
*
from
tb t
where
not exists(select 1 from tb where a>t.a)
#2
select * from table1 where not exists(select 1 from tb where a>table1.a)
#3
declare @a int
select @a=max(a) from tb
select * from tb where a=@a
select @a=max(a) from tb
select * from tb where a=@a
#4
CREATE TABLE TEST(ID INT,NAME VARCHAR(20))
INSERT INTO TEST SELECT 1,'LIHAN' UNION ALL
SELECT 2,'LLLLL' UNION ALL
SELECT 3,'IIIII' UNION ALL
SELECT 3,'NNNNN'
GO
SELECT * FROM TEST
SELECT * FROM TEST T WHERE NOT EXISTS(SELECT * FROM TEST WHERE ID>T.ID)
ID NAME
----------- --------------------
1 LIHAN
2 LLLLL
3 IIIII
3 NNNNN
(所影响的行数为 4 行)
ID NAME
----------- --------------------
3 IIIII
3 NNNNN
(所影响的行数为 2 行)
#5
select top 1 * from tablename order by a desc
#6
if object_id('tempdb..#t_temp') is not null
drop table #t_temp
create table #t_temp
(
a varchar(10) null,
b varchar(10) null,
c int
);
insert into #t_temp (a,b,c) values ('aa','bb',123333);
insert into #t_temp (a,b,c) values ('bbbb','ccccc',1223);
insert into #t_temp (a,b,c) values ('aa','asdsad',1231);
insert into #t_temp (a,b,c) values ('23','dd',5123);
insert into #t_temp (a,b,c) values ('as','cc',8677);
insert into #t_temp (a,b,c) values ('dff','cc',12223);
SELECT * FROM #t_temp a WHERE c IN
(SELECT TOP 1 c FROM #t_temp ORDER BY c desc)
#7
结果是
/*
aa bb 123333
*/
T.T 早上属于抢分高峰期...
#8
select * from table1 t1 where t1.a =(select max(t2.a) from table1 t2);
#9
SELECT * FROM TB WHERE A IN (SELECT MAX(A) FROM TB)
#10
先排序,再选就快了
#11
方法很多的。
SELECT * FROM TABLE1 WHERE not EXISTS(SELECT 1 FROM TABLE1 tb where a>table1.a)
SELECT * FROM TABLE1 WHERE A = (SELECT MAX(A) FROM TABLE1)
#12
select * from t where a in (select MAX(a) from t)
#1
select
*
from
tb t
where
not exists(select 1 from tb where a>t.a)
#2
select * from table1 where not exists(select 1 from tb where a>table1.a)
#3
declare @a int
select @a=max(a) from tb
select * from tb where a=@a
select @a=max(a) from tb
select * from tb where a=@a
#4
CREATE TABLE TEST(ID INT,NAME VARCHAR(20))
INSERT INTO TEST SELECT 1,'LIHAN' UNION ALL
SELECT 2,'LLLLL' UNION ALL
SELECT 3,'IIIII' UNION ALL
SELECT 3,'NNNNN'
GO
SELECT * FROM TEST
SELECT * FROM TEST T WHERE NOT EXISTS(SELECT * FROM TEST WHERE ID>T.ID)
ID NAME
----------- --------------------
1 LIHAN
2 LLLLL
3 IIIII
3 NNNNN
(所影响的行数为 4 行)
ID NAME
----------- --------------------
3 IIIII
3 NNNNN
(所影响的行数为 2 行)
#5
select top 1 * from tablename order by a desc
#6
if object_id('tempdb..#t_temp') is not null
drop table #t_temp
create table #t_temp
(
a varchar(10) null,
b varchar(10) null,
c int
);
insert into #t_temp (a,b,c) values ('aa','bb',123333);
insert into #t_temp (a,b,c) values ('bbbb','ccccc',1223);
insert into #t_temp (a,b,c) values ('aa','asdsad',1231);
insert into #t_temp (a,b,c) values ('23','dd',5123);
insert into #t_temp (a,b,c) values ('as','cc',8677);
insert into #t_temp (a,b,c) values ('dff','cc',12223);
SELECT * FROM #t_temp a WHERE c IN
(SELECT TOP 1 c FROM #t_temp ORDER BY c desc)
#7
结果是
/*
aa bb 123333
*/
T.T 早上属于抢分高峰期...
#8
select * from table1 t1 where t1.a =(select max(t2.a) from table1 t2);
#9
SELECT * FROM TB WHERE A IN (SELECT MAX(A) FROM TB)
#10
先排序,再选就快了
#11
方法很多的。
SELECT * FROM TABLE1 WHERE not EXISTS(SELECT 1 FROM TABLE1 tb where a>table1.a)
SELECT * FROM TABLE1 WHERE A = (SELECT MAX(A) FROM TABLE1)
#12
select * from t where a in (select MAX(a) from t)