简体   繁体   中英

How to get all rows of one table after joining?

I have a database with 3 tables: students , courses and mistakes . I have one joining table ( csm ) where I connect the 3 tables. I am supposing mistakes are the same for each course.

Table Courses

+----------+---------------+
| crs_id   | crs_name      |
+----------+---------------+
| 1        | HTML          |
| 2        | PHP           |
| 3        | Python        |
+----------+---------------+

Table Students

+----------+---------------+---------------+
| stu_id   | stu_firstname | stu_lastname  |
+----------+---------------+---------------+
| 1        | Tina          | Turner        |
| 2        | Lisa          | Laroi         |
| 3        | Dina          | Donna         |
| 3        | Jim           | Leduc         |
+----------+---------------+---------------+

Table Mistakes

+----------+---------------+------------+
| mis_id   | mis_name      | mis_weight |
+----------+---------------+------------+
| 1        | No camelCase  | 7          |
| 2        | No brackets   | 10         |
| 3        | Operator mist.| 12         |
+----------+---------------+------------+

Joining table CSM

+----------+------------+------------+------------+
| csm_id   | fk_crs_id  | fk_stu_id  | fk_mis_id  |
+----------+------------+------------+------------+
| 1        | 1          | 1          | 1          |
| 2        | 1          | 1          | 3          |
| 3        | 2          | 3          | 1          |
| 4        | 3          | 2          | 2          |
| 5        | 3          | 2          | 1          |
| 6        | 3          | 3          | 1          |
+----------+------------+------------+------------+

If I select a specific course, I want to get a list of ALL students with the minus points for this course. So I also want to get the students with no result in the joining table csm.

The closest result I got is with the following sql statement:

select stu_firsname, stu_lastname, csm.*, sum(mis_weight) 
from students s
left join crs_stu_mis csm on s.stu_id = csm.fk_stu_id
left join mistakes m on csm.fk_mis_id = m.mis_id
where fk_crs_id = 4 or fk_crs_id is null
group by stu_firstname;

With this I get the sum of the mistakes for a certain course and also the students who don't have any records in CSM table, but some results are missing. For example, this doesn't show the students who have records in the CSM table, but not for the requested course.

How do I get these students in my result table?

if your primary data's is in CSM table, try this:

select s.stu_firsname, s.stu_lastname, csm.*, sum(m.mis_weight) from crs_stu_mis csm
join  students s on s.stu_id = csm.fk_stu_id
join mistakes m on csm.fk_mis_id = m.fou_id
csm.fk_crs_id = 4
group by s.stu_naam;

Second case: Your data's can affected by group by attribute, try attribute that it is not NULL , ex:

select s.stu_firsname, s.stu_lastname, csm.*, sum(m.mis_weight) from crs_stu_mis csm
join  students s on s.stu_id = csm.fk_stu_id
join mistakes m on csm.fk_mis_id = m.fou_id
csm.fk_crs_id = 4
group by csm.csm_id;

In your query :

left join crs_stu_mis csm on s.stu_id = csm.fk_stu_id`
...
where fk_crs_id = 4 or fk_crs_id is null

This is not exactly what you want, since this condition will filter out students that have records in the csm table for only courses other than 4 . You want to move that condition to the corresponding LEFT JOIN :

left join crs_stu_mis csm on s.stu_id = csm.fk_stu_id AND csm.fk_crs_id = 4

Another potential source of problems is the way the query handles aggregation. There are non-aggregated columns in the SELECT clause that do not appear in the GROUP BY clause. This syntax is not good SQL coding practice, and is not supported anymore since version 5.7 of MySQL. I assumed that you want to one record in the result for each student.

Query:

select 
    s.stu_firstname, 
    s.stu_lastname, 
    sum(m.mis_weight) total_misses_weight
from 
    students s
    left join crs_stu_mis csm on s.stu_id = csm.fk_stu_id AND csm.fk_crs_id = 3
    left join mistakes m on csm.fk_mis_id = m.mis_id
group by 
    s.stu_id, 
    s.stu_firstname, 
    s.stu_lastname

Demo on DB Fiddle for course id 3 :

| stu_firstname | stu_lastname | total_misses_weight |
| ------------- | ------------ | ------------------- |
| Tina          | Turner       |                     |
| Lisa          | Laroi        | 17                  |
| Dina          | Donna        | 7                   |
| Jim           | Leduc        |                     |

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