简体   繁体   中英

MySql Query for one to many relationship

Facing issue in mysql query, tried with mysql join but not getting expected output.

I want all class, all student record with total ratingscore.Each Class has Many Student. Student has Many or none scholarship

Class table looks like this

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

Student table looks like, classid is foreign key

+------------------------------------+
|   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      |
+------------------------------------+

rating table looks like, studentid is foreign key

+------------------------------------+
|   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       |
+------------------------------------+

Here : No student from class 3, got any ratingscore yet.so need that student has zero ratingscore.

Expected Output:-

+-------------------------------------------------------------+
|   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     |
+-------------------------------------------------------------+
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

Have you try this ?

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

If there is student without class, you have to change inner join to left outer join

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM