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

xq_zhang 2007-06-25 02:30:47

我有一个表结构如下:
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
...全文
139 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xq_zhang 2007-06-25
  • 打赏
  • 举报
回复
多谢大家各位,我先试一下。
hongqi162 2007-06-25
  • 打赏
  • 举报
回复
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
不懂编程 2007-06-25
  • 打赏
  • 举报
回复
错了,不是=,是in,select * from table A where id in (select max(id) from table B group by b.ip)
不懂编程 2007-06-25
  • 打赏
  • 举报
回复
select * from table A where id = (select max(id) from table B group by b.ip)
xq_zhang 2007-06-25
  • 打赏
  • 举报
回复
顶起来。大家来帮帮我呀!

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧