日期:2014-05-17  浏览次数:20711 次

求一条sql查询语句,请大家多多指点。

我有一个表结构如下:
id               ip                 name       time
1         10.0.0.1     aa           2007-06-01
2         10.0.0.2     bb           2007-06-01
3         10.0.0.1     aa           2007-06-02
4         10.0.0.2     bb           2007-06-02
5         10.0.0.3     cc           2007-06-03

在我的表中有一个ip字段
我想显示出每个ip的最新纪录
想要得到结果如下:
id       ip                 name       time
3         10.0.0.1     aa           2007-06-02
4         10.0.0.2     bb         2007-06-02
5         10.0.0.3     cc           2007-06-03

------解决方案--------------------
错了,不是=,是in,select * from table A where id in (select max(id) from table B group by b.ip)

------解决方案--------------------
create table t22(id int,ip varchar2(100), name varchar2(100), time date)
insert into t22
select 1, '10.0.0.1 ', 'aa ',to_date( '2007-06-01 ', 'yyyy-mm-dd ') from dual union all
select 2, '10.0.0.2 ', 'bb ',to_date( '2007-06-01 ', 'yyyy-mm-dd ') from dual union all
select 3, '10.0.0.1 ', 'aa ',to_date( '2007-06-02 ', 'yyyy-mm-dd ') from dual union all
select 4, '10.0.0.2 ', 'bb ',to_date( '2007-06-02 ', 'yyyy-mm-dd ') from dual union all
select 5, '10.0.0.3 ', 'cc ',to_date( '2007-06-03 ', 'yyyy-mm-dd ') from dual
/
--执行查询
select t.* from t22 t
inner join
(select max(time) time,name from t22 group by name )t1
on t.time=t1.time and t.name=t1.name
--查询结果
3 10.0.0.1 aa 2007-6-2
4 10.0.0.2 bb 2007-6-2
5 10.0.0.3 cc 2007-6-3