我在db中有这种类型的关系。
我编写了这样的SQL代码来获取用户的平均评分列表。
SELECT
c.id,l.login,
AVG(f.mark) AS AVGRating
FROM logininfo l
INNER JOIN person p
ON l.id = p.info_id
JOIN candidate c
ON p.id = c.id
JOIN feedback f
GROUP BY c.id
ORDER BY AVGRating ASC;
问题是我为所有用户获得相同的评分。
答案 0 :(得分:2)
您错过了反馈表的加入条件:
SELECT
c.id,l.login,
AVG(f.mark) AS AVGRating
FROM logininfo l
JOIN person p
ON l.id = p.info_id
JOIN candidate c
ON p.id = c.id
JOIN feedback f
ON c.id = f.candidat_id -- < -- this one
GROUP BY c.id
ORDER BY AVGRating ASC;
顺便说一下,INNER JOIN
和JOIN
是相同的。
答案 1 :(得分:0)
SELECT
c.id,l.login,
AVG(f.mark) AS AVGRating
FROM logininfo l
INNER JOIN person p
ON l.id = p.info_id
JOIN candidate c
ON p.id = c.id
JOIN feedback f
--add a join condition
GROUP BY c.id
ORDER BY AVGRating ASC;