Oracle数据库的SQL查询语句查询每个用户最近一次的登录记录并且只显示一条

来源:互联网 发布:xbox网络teredo不合格 编辑:程序博客网 时间:2024/05/16 09:50
有如下数据库及数据:
create table oness(       oids number(4) primary key,       Names varchar2(10) not null,       Email varchar2(20),       LastLogin date);

添加的数据如下:


查询出test4、test1、test2用户最近的登录记录有两种方式:

第一种:

select * from oness awhere not exists(select 1 from oness where Names = a.Names and LastLogin > a.LastLogin);
查询结果为:

第二种:

select * from oness a where LastLogin=(select max(LastLogin) from oness where Names=a.Names);
查询结果为:

以上两种方式查询基本一样,都可以,只不过查询后的结果顺序不同。


1 0