请教各位老大。我是初学数据库的以下的问题怎么做? 谢谢了
--------------------------------
addr | name | time | dr |
--------------------------------
shanghai | a | 10 | q |
--------------------------------
shanghai | b | 20 | r |
--------------------------------
shanghai | c | 16 | i |
--------------------------------
beijing | q | 16 | j |
--------------------------------
beijing | m | 26 | op |
--------------------------------
数据表如上,要求取出符合以下条件的记录:
1、取出以addr字段分组time字段值为最大的记录
取出的记录为:
--------------------------------
shanghai | b | 20 | r |
--------------------------------
beijing | m | 26 | op |
--------------------------------
要求写出SQL语句
to:pengdali
你昨天说的好像也不对啊,你的方法查出所有的记录了。
select * from 表 where time=(select max(time) from 表 别名 where 表.addr=别名.addr)
select a.* from tablename as a
where a.time =
(select max(time) from tablename
where addr = A.addr)
select table.* from table ,(select addr, max(time) as time from table group by addr) t1 where table.addr=t1.addr and table.time=t1.time
SELECT * FROM yOURTABLE WHERE ADDR + STR(TIME) IN (SELECT ADDR + STR(MAX(TIME)) FROM Yourtable GROUP BY addr)
select * from 表 a where exists (select 1 from (select addr,max(time) time from 表 group by addr) b where a.addr=b.addr and a.time=b.time)
select * from tablename a
where time in
(
select max(time) from tablename b
where a.addr = b.addr
group by addr
)
select addr,max(time) from table group by addr
select a.addr,
(select top 1 name from table4 where time = max(a.time)),
max(a.time),
(select top 1 dr from table4 where time = max(a.time))
from table4 a group by a.addr