我想找到经验最丰富的用户(最常见的贡献者)。
SELECT X.userid, count(X.id) as jobs, SUM(X.stars) as stars
FROM experiences AS X
RIGHT JOIN users AS U ON U.userid = X.userid
经验表:
| id | userid | stars | ...
1 1 3
2 1 4
用户表:
| userid | username | password | photo | ...
1 goofy 12345678 goofy.png
2 donald 12345678 dduck.png
3 pluto 12345678 pluto.png
问题是,结果回来了但是加入没有加入。我得到的是:
结果:
userid | jobs | stars |
1 2 7
我正在寻找的是:
userid | jobs | stars | username | password | photo | ...
1 2 7 goofy 12345678 goofy.png
答案 0 :(得分:1)
您的初始SELECT
语句处理该问题。数据在那里,你只需要选择"它
基本上只是:
SELECT X.userid, count(X.id) as jobs, SUM(X.stars) as stars, U.*
U.*
将再次包含用户ID,或将其范围限定为username, password, photo, ...
答案 1 :(得分:1)
加入会不会自动影响您的选择列表。如果您想要连接的列,则必须明确选择它们:
SELECT X.userid, count(X.id) as jobs, SUM(X.stars) as stars, U.*
-- Here ----------------------------------------------^
FROM experiences AS X
RIGHT JOIN users AS U ON U.userid = X.userid