I want to achieve a function in MySQL like 
select * from (
    select *, row_number() over (partition by uid order by last_login desc) as rn 
    from test) 
where rn = 1
Then I found a magical SQL as follows:
select * from test as a 
where typeindex = (select max(b.last_login) 
                   from test as b 
                   where a.uid = b.uid);
It can fit my requirement and efficiency is good but it's a bit difficult for me to understand the procedure of this query particularly the where a.uid = b.uid in the sub-query.
Could any body give a brief flow of how this query works? Thanks for your help in advance.
 
    