Login table, query logged in user name
select username
from system_online_users
group by username;
Simply query the last login time of each user
select username, max(login_time)
from system_online_users
group by username;
Query admin
the earliest and last login time
select username, min(login_time) min_login_tiem, max(login_time) max_login_tiem
from system_online_users
where username = 'admin'
group by username;
As shown in the picture:
More nicknames have been queried, and group by
need to be added innickname
select username, max(login_time), nickname
from system_online_users
group by username, nickname;
Do not want group by
to add in nickname
, you can write directly when queryingmax(nickname)
The premise is to ensure the uniqueness of username
andnickname
However, this approach is not very friendly, and the user may have modified the nickname midway, resulting in the fact that the queried nickname may not be the latest nickname
select username, max(login_time), max(nickname)
from system_online_users
group by username;
It is strongly recommended to use inline query, you don’t need to write multiple fields group by
, and you don’t need to write multiple fields if you want to checkmax()
select t.username, t.login_time, t.nickname, t.ip, t.os
from system_online_users t
inner join (select username, max(login_time) login_time from system_online_users group by username) t1
on t.username = t1.username and t.login_time = t1.login_time;