主表:
table1 (uniqueid,name)
table2(uniqueid,tbl1_uniqueid,loginTime)
table1 数据
uniqueid name
1 张三
2 李四
3 王武
table2 数据
uniqueid tbl1_uniqueid loginTime
1 1 2015-7-12
2 1 2015-7-15
3 1 2015-7-21
4 2 2015-7-9
查询结果为:
uniqueid name loginTime
1 张三 2015-7-21
2 李四 2015-7-9
sql语句为:
select a.uniqueid as uniqueid ,a.name as name ,b.loginTime as loginTime
from table1 as a
left join
table2 as b
on a.uniqueid = b.tbl1_uniqueid
where b.loginTime in (select max(loginTime) from table2 group by tbl1_uniqueid)