/*
select user_id, max(data) as d 
from login
group by user_id;
*/
select u.name as u_n, c.name as c_n, ud.d as u_date
from client as c, user as u, login as l,
    (select user_id, max(date) as d from login group by user_id) as ud
where ud.user_id = l.user_id
and ud.d = l.date
and ud.user_id = u.id
and l.client_id = c.id
order by u.name;

在已知没有捷径的情况下,

第一步,先从简单的步骤做起,一层一层的套娃。

第二步,观察有无明显的规律,尤其是存在多个表时。

tips:聚合函数的查询语句中,只能包括聚合列和分组列。