简体   繁体   English

如何在顶部和底部显示在线用户呢?

[英]How to show online users in the top and offline after it?

Could you guys please tell me how to show all online people on the top and all offline after, both lists in ascending order of the username just like Skype does. 你们能否告诉我如何在顶部显示所有在线用户,然后再显示所有离线用户,就像Skype一样,这两个用户名都按升序排列。

My MySQL table is Users, and the columns are as follow: 我的MySQL表是Users,列如下:

ID
Username
Last_logged_in - is with unix_timestamp();

the code I am using to show online people is as follow: 我用来显示在线用户的代码如下:

select * from users where Last_logged_in >= UNIX_TIMESTAMP() - 60;

and the code I tried was like so: 我尝试的代码是这样的:

select id, username,  last_logged_in from users where last_logged_in >= UNIX_TIMESTAMP() - 60 UNION select id, username,  last_logged_in from users where  last_logged_in <= UNIX_TIMESTAMP() - 60 LIMIT 10;

some helps will be really appreciated. 一些帮助将不胜感激。

No need for union, just order by the last_logged_in field descending, since you use this field to calculate if a user is logged in or not: 不需要联合,只需按last_logged_in字段降序排列,因为您使用此字段来计算用户是否登录:

select * from users order by Last_logged_in desc limit 10;

This way the logged in users will be on the top of the list because they loggen in most recently. 这样,已登录的用户将在列表的顶部,因为它们是最近登录的用户。

UPDATE 更新

Still no need for union if you want to order both lists by username, let's build on axiac's proposed solution: 如果您想按用户名订购两个列表,仍然不需要联合,让我们以axiac提出的解决方案为基础:

SELECT u.*, IF(Last_logged_in >= UNIX_TIMESTAMP() - 60, 1, 0) as isOnline;
FROM users u
ORDER BY isOnline DESC, u.Username ASC

Your query doesn't provide the results you expect because UNION does not preserve the order of the rows from the queries it unions. 您的查询没有提供您期望的结果,因为UNION不会保留它合并的查询中行的顺序

You need to enclose each of the unioned queries into parentheses and add ORDER BY after the last query. 您需要将每个联合查询括在括号中,并在最后一个查询之后添加ORDER BY However, if you do that you obtain a big and slow query that selects all the records from table users in a convoluted way. 但是,如果这样做,您将获得一个庞大而缓慢的查询,该查询会以复杂的方式从表users中选择所有记录。

The solution is to order the rows descending by column Last_logged_in . 解决方案是按行Last_logged_in降序对行进行Last_logged_in This way, the users that logged in recently are returned first. 这样,将首先返回最近登录的用户。 Also you need to add in the fields list the expression that tells if the user is still online. 另外,您需要在字段列表中添加表示用户是否仍在线的表达式。 Finally, because your query does not filter the returned users, a LIMIT clause is recommended; 最后,由于您的查询没有过滤返回的用户,因此建议使用LIMIT子句。 otherwise the query will return the entire table which is probably not what you want. 否则查询将返回可能不是您想要的整个表。

Update 更新资料

As the OP specified in a comment , the users must be sorted by their online status (online first) and then by their usernames (ascending). 正如注释中指定的OP一样,用户必须按其在线状态(首先在线)然后按其用户名(升序)进行排序。

The (updated) query is: (更新的)查询是:

SELECT u.*, IF(Last_logged_in >= UNIX_TIMESTAMP() - 60, 1, 0) as isOnline;
FROM users u
ORDER BY isOnline DESC, username ASC
LIMIT 20

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM