简体   繁体   English

加入后如何获取一个表的所有行?

[英]How to get all rows of one table after joining?

I have a database with 3 tables: students , courses and mistakes . 我有一个包含3个表的数据库: studentscoursesmistakes I have one joining table ( csm ) where I connect the 3 tables. 我有一个连接表( csm ),我在其中连接3个表。 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

+----------+------------+------------+------------+
| 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. 因此,我也想让加入表csm中没有结果的学生。

The closest result I got is with the following sql statement: 我得到的最接近的结果是以下SQL语句:

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. 这样,我就可以得出某门课程的错误总和,以及在CSM表中没有任何记录但缺少一些结果的学生。 For example, this doesn't show the students who have records in the CSM table, but not for the requested course. 例如,这不会显示在CSM表中有记录的学生,但不会显示所请求课程的学生。

How do I get these students in my result table? 如何在成绩表中找到这些学生?

if your primary data's is in CSM table, try this: 如果您的主要数据在CSM表中,请尝试以下操作:

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: 第二种情况:您的数据会受到group by属性的影响,请尝试将其不是NULL的try属性,例如:

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 . 这并不是您想要的,因为这种情况将过滤出在csm表中仅记录了4以外课程的学生。 You want to move that condition to the corresponding LEFT JOIN : 您想要将该条件移至相应的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. SELECT子句中有未聚合的列,它们没有出现在GROUP BY子句中。 This syntax is not good SQL coding practice, and is not supported anymore since version 5.7 of MySQL. 此语法不是良好的SQL编码实践,并且自MySQL 5.7版以来不再受支持。 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 : 关于DB Fiddle的课程ID 3 演示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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