Mysql 正在查找有关SQL查询的帮助

Mysql 正在查找有关SQL查询的帮助,mysql,sql,database,optimization,Mysql,Sql,Database,Optimization,我正在寻找从我的sql表中检索数据的更好方法 Table 1: User data - User Id - User Created date Table 2: Mapping of the user with a role - User Id - Role Table 3 Role definition Table 4 (may or may not have user data based on his activities on the site) User data Eg. -

我正在寻找从我的sql表中检索数据的更好方法

Table 1: User data
- User Id
- User Created date

Table 2: Mapping of the user with a role
- User Id
- Role

Table 3 
Role definition

Table 4 (may or may not have user data based on his activities on the site)
User data
Eg.
- User Id
- Total counts of the number of visits made on the portal
我希望写最少数量的查询(最好是1)来完成以下工作

*我想打印总计数最高的每种角色类型的顶级用户*

输出内容如下所示:

Header UserId---Rolename--Total Count
Row1   Test1 ---Staff   --1293
Row2   Test2 ---Faculty --1223
Row3   Test3 ---Dean    --2283928

有什么建议吗?

这就是你想要的:

SELECT a.UserId, b.Role, c.TotalCount
FROM TABLE1 as a join Table2 as b on a.UserId = b.UserId
                 join Table 4 as c on a.UserId = c.UserId
ORDER BY c.TotalCount DESC
LIMIT 3
考虑提供DDLs(和/或SQLFIDDLE)连同上面的代表性数据集和期望的结果集。