我有一个问题,是否可以根据同一查询中的另一个表值从一个表中进行选择。就我而言,我有两个名为follow
和score
的表。
请
------------------------
| follower | following |
------------------------
| Uname2 | Uname1 |
------------------------
得分
--------------------
| username | score |
--------------------
| Uname1 | 1 |
--------------------
| Uname1 | 2 |
--------------------
我尝试共犯的方法是首先获得用户关注的对象,然后获得该用户的总分。我已尝试使用内连接等但无法使其工作。
查询
"SELECT follow.following, score.SUM('score') FROM follow INNER JOIN score ON follow.following=score.username WHERE follow.follower='Uname2'";
答案 0 :(得分:1)
要获得一条记录,请使用:
SELECT
f.following,
SUM(s.score) as score
FROM
Follow f, Score s
WHERE
f.following = s.username
AND f.follower = 'Uname2'
获取每次尝试的总和:
SELECT
f.follower,
f.following,
SUM(s.score) as score
FROM
Follow f, Score s
WHERE
f.following = s.username
GROUP BY f.follower
这是一个Fiddle示例。
答案 1 :(得分:0)
您可能需要在此处添加GROUP BY
子句,在查询末尾添加GROUP BY follow.following
。
答案 2 :(得分:0)
像这样使用 GROUP BY 。希望它会有所帮助。
SELECT follow.following, score.SUM('score') FROM follow INNER JOIN score ON follow.following=score.username WHERE follow.follower='Uname2' GROUP BY follow.following