不想取到一组记录后用order by的方法,不知道有没有其他的方法,让数据库仅返回该条符合条件的纪录,比如max()?例如,有下面这个表,记录这个月发生的各种出差费用,每一笔出差费用对应一条记录。如何查到张三这个月出差费用支持最多的那条记录?人名 费用 地点 时间张三 500 武汉 5.11
李四 300 武汉 5.5
张三 700 广州 5.20
王五 800 上海 5.27
李四 400 北京 5.26
张三 550 成都 5.12则所期望得到的张三的纪录为
张三 700 广州 5.20谢谢各位答人
李四 300 武汉 5.5
张三 700 广州 5.20
王五 800 上海 5.27
李四 400 北京 5.26
张三 550 成都 5.12则所期望得到的张三的纪录为
张三 700 广州 5.20谢谢各位答人
from 表
where 费用 =(select max(费用) from 表 )
from 表 as t
where 费用 =(select max(费用) from 表 where 人名=t.人名)
and 人名='张三'
(
select max(费用) 费用 from tb where 人名 = '张三'
) b
where 人名 = '张三' and a.费用 = b.费用
insert into tb values('张三', 500, '武汉', '5.11')
insert into tb values('李四', 300, '武汉', '5.5')
insert into tb values('张三', 700, '广州', '5.20')
insert into tb values('王五', 800, '上海', '5.27')
insert into tb values('李四', 400, '北京', '5.26')
insert into tb values('张三', 550, '成都', '5.12')
select a.* from tb a,
(
select max(费用) 费用 from tb where 人名 = '张三'
) b
where 人名 = '张三' and a.费用 = b.费用drop table tb/*
人名 费用 地点 时间
---------- ----------- ---------- ----------
张三 700 广州 5.20(所影响的行数为 1 行)
*/
from 表 as t
where 费用 =(select max(费用) from 表 where 人名=t.人名)
and 人名='张三'
--或select *
from 表 as t
where 人名='张三' and not exists (select * from 表 where 人名=t.人名 and 费用>t.费用)
insert into tb values('张三', 500, '武汉', '5.11')
insert into tb values('李四', 300, '武汉', '5.5')
insert into tb values('张三', 700, '广州', '5.20')
insert into tb values('王五', 800, '上海', '5.27')
insert into tb values('李四', 400, '北京', '5.26')
insert into tb values('张三', 550, '成都', '5.12')
select a.* from tb a,
(
select 人名,max(费用) 费用 from tb group by 人名
) b
where a.人名 = b.人名 and a.费用 = b.费用drop table tb/*
人名 费用 地点 时间
---------- ----------- ---------- ----------
张三 700 广州 5.20
王五 800 上海 5.27
李四 400 北京 5.26(所影响的行数为 3 行)
*/
and 人名='张三'
(
select max(费用) 费用 from tb where 人名 = '张三'
) b
where 人名 = '张三' and a.费用 = b.费用
where 人名 = '张三' and a.费用 = b.费用
from 表 as t
where 费用 =(select max(费用) from 表 where 人名=t.人名 and date1='2007-08')
and 人名='张三'
and date1='2007-08'
where col in (select max(col) from tab)
select *
from 表 as t
where 费用 in (select max(费用) from 表 where 人名=t.人名)
and 人名='张三'---2
select *
from 表 as t
where not exists(select 1 from 表 where 人名=t.人名 and t.费用<费用)
and 人名='张三'