MySQL ORDER BY 另一个表中的最高行数 [英] MySQL ORDER BY highest number of rows in another table
本文介绍了MySQL ORDER BY 另一个表中的最高行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
MySQL ORDER BY 另一个表中用户的总行数一个>
我有 5 个用户(在表 users
上):
I have 5 users (on table users
):
User ID (uid) | User Name (uname)
-----------------------------------
1 Fallon
2 Jeremy
3 Matt
4 Peter
5 John
- Fallon (uid 1) 迄今已登录 35 次.
- Jeremy 登录了 32 次.
- 马特登录了 57 次.
- Peter 登录了 43 次.
- 约翰登录了 23 次.
日志详细信息存储在另一个表中(logs
):
Log details are stored in another table (logs
):
Log ID (logid) - User ID (uid)
现在我想按登录次数最多的顺序表示用户列表.登录最多的用户将位于顶部,第二个登录最多的用户将位于第二位.喜欢:
Now I want to represent a list of users in order of most logged times. The user who logged in the most will be at the top, second most logged user will be the second. Like:
- 马特登录了 57 次.
- Peter 登录了 43 次.
- Fallon 登录了 35 次.
- Jeremy 登录了 32 次.
- 约翰登录了 23 次.
因为日志存储在一个表中,而用户详细信息存储在另一个表中.我如何,我应该使用哪些查询来制作上述列表.请帮忙.
Because Logs are stored in one table and User details in another. How can I, What queries should I, use to make a list like the above. Please help.
推荐答案
这只是一个涉及 JOIN
的简单查询:
It's just a simple query involving a JOIN
:
SELECT users.uid, users.uname, COUNT(logs.logid) AS logins
FROM users
LEFT JOIN logs ON logs.uid=users.uid
GROUP BY users.uid
ORDER BY logins DESC
这篇关于MySQL ORDER BY 另一个表中的最高行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文