0

我正在寻找一种从我的 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

有什么建议么?

4

1 回答 1

0

这是你要找的:

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
于 2013-04-02T14:35:19.190 回答