如何获得每个用户的评分?

时间:2015-07-29 20:14:12

标签: mysql sql

我在db中有这种类型的关系。 db relations image

我编写了这样的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; 

问题是我为所有用户获得相同的评分。

2 个答案:

答案 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 JOINJOIN是相同的。

答案 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;