MySql查询一对多关系

时间:2016-12-17 09:36:48

标签: php mysql

面对mysql查询中的问题,尝试使用mysql join但没有得到预期的输出。

我想要所有班级,所有学生的总评分记录。每个班级都有很多学生。学生有很多或没有奖学金

类表看起来像这样

+---------------------+
|   id      classname |
+---------------------+
|   1            10   |
|   2            11   |
|   3            12   |
+---------------------+

学生表看起来像,classid是外键

+------------------------------------+
|   id     classid       studentname |
+------------------------------------+
|   1        1            xembine    |
|   2        1            denial     |
|   3        2            suzone     |
|   4        3            rosh       |
|   5        2            broad      |
|   6        1            bell       |
|   7        3            martin     |
|   8        1            jroff      |
+------------------------------------+

评级表看起来像,studentid是外键

+------------------------------------+
|   id      studentid    ratingscore |
+------------------------------------+
|   1           1         4000       |
|   2           1         10000      |
|   3           5         20000      |
|   4           2         1000       |
|   5           6         2222       |
|   6           1         5000       |
|   7           6         12000      |
|   8           3         3800       |
|   9           5         7500       |
+------------------------------------+

这里:没有来自3级的学生,还没有得到任何评分。所以要求学生的评分为零。

预期产出: -

+-------------------------------------------------------------+
|   studentname  studentid  classid   classname   ratingscore |
+-------------------------------------------------------------+
|     xembine       1         1           10         19000    |
|     denial        2         1           10          1000    |
|     suzone        3         2           11          3800    |
|     rosh          4         3           12            0     |
|     broad         5         2           11          27500   |
|     bell          6         1           10          2222    |
|     martin        7         3           12            0     |
|     jroff         8         1           10            0     |
+-------------------------------------------------------------+

3 个答案:

答案 0 :(得分:1)

select s.studentname, s.id as studentid,s.classid,c.classname,sum(ifnull(r.ratingscore,0)) as ratingscore from student s
join class c on c.id=s.classid
left outer join rating r on r.studentid=s.id
group by s.studentname,r.studentid,s.classid,c.classname

答案 1 :(得分:0)

你试试这个吗?

SELECT s.studentname,  s.studentid, c.classid, c.classname, SUM(r.ratingscore)
  FROM student as s
INNER JOIN class c on c.classid = s.classid
LEFT OUTER JOIN ratingscore rs ON s.studentid = rs.studentid
GROUP BY s.studentname,  s.studentid, c.classid, c.classname
ORDER BY s.studentid

如果学生没有上课,则必须将内部联接更改为左外部联接

答案 2 :(得分:0)

SELECT s.studentname AS studentname,  s.id AS studentid, c.id AS classid, c.classname AS classname, SUM(r.ratingscore) AS ratingscore
FROM student AS s
INNER JOIN class AS c ON c.id = s.classid
LEFT JOIN rating r ON r.studentid = s.id
GROUP BY s.id
ORDER BY s.id