如何只选择A中的条目,在B表的所有各行中只有值= X?

时间:2018-04-17 12:17:24

标签: mysql join

如何只选择那些将所有实习生的'Practice_result.Mark等于5?的mentors.l_name,mentors.f_name?

目前,我只能使用以下内容为所有“导师”选择所有“实习生”的所有分数:

SELECT mentors.l_name, mentors.f_name, interns.l_name, Practice_result.Mark
FROM mentors

LEFT OUTER JOIN interns_specialty
ON mentors.mentor_id = interns_specialty.mentor_id

LEFT OUTER JOIN interns
ON interns.intern_id = interns_specialty.intern_id

LEFT OUTER JOIN Practice_result
ON Practice_result.intern_id = interns.intern_id
;

结果如下:

+---------+----------+---------+------+
| l_name  | f_name   | l_name  | Mark |
+---------+----------+---------+------+
| Mentor1 | Mentor1  | Intern1 | 5    |
| Mentor2 | Mentor2  | Intern2 | 4    |
| Mentor1 | Mentor1  | Intern3 | 5    |
| Mentor3 | Mentor3  | Intern4 | NULL |
| Mentor2 | Mentor2  | Intern5 | 3    |
| Mentor3 | Mentor3  | Intern6 | 5    |
| Mentor4 | Mentor4  | Intern7 | 4    |
| Mentor4 | Mentor4  | Intern8 | 5    |
+---------+----------+---------+------+

或者我可以选择所有ROWS与Practice_result.Mark ='5'而不排除那些也有实习生的实习生,他们也有实习生。马克<> '5'。

我需要排除那些<> 5,所以在这种情况下只返回Mentor1,因为只有他的'实习生'才有5s。

我尝试使用ORDER BY和LIMIT 1,但仍然没有得到如何让它适用于较小的同一个mentor.l_name,但是所有的实习生。

表: 向导:

+-----------+----------+---------+
| mentor_id | f_name   | l_name  |
+-----------+----------+---------+
|         1 | Mentor2  | Mentor2 |
|         2 | Mentor1  | Mentor1 |
|         3 | Mentor3  | Mentor3 |
|         4 | Mentor4  | Mentor4 |
+-----------+----------+---------+

实习生:

+-----------+----------+---------+
| intern_id | f_name   | l_name  |
+-----------+----------+---------+
|         1 | Name1    | Intern1 |
|         2 | Name2    | Intern2 |
|         3 | Name3    | Intern3 |
|         4 | Name4    | Intern4 |
|         5 | Name5    | Intern5 |
|         6 | Name6    | Intern6 |
|         7 | Name7    | Intern7 |
|         8 | Name8    | Intern8 |
|         9 | Name9    | Intern9 |
|        10 | Name10   | Intern10|
|        11 | Name11   | Intern11|
+-----------+----------+---------+

interns_specialty:

 +-----------+--------------+-----------+
    | intern_id | specialty_id | mentor_id |
    +-----------+--------------+-----------+
    |         1 | 1            |         2 |
    |         2 | 1            |         1 |
    |         3 | 4            |         2 |
    |         4 | 2            |         3 |
    |         5 | 3            |         1 |
    |         6 | 3            |         3 |
    |         7 | 4            |         4 |
    |         8 | 4            |         4 |
    +-----------+--------------+-----------+

Practice_result:

+-----------+------+
| intern_id | Mark |
+-----------+------+
|         1 | 5    |
|         2 | 4    |
|         3 | 5    |
|         5 | 3    |
|         6 | 5    |
|         7 | 4    |
|         8 | 5    |
+-----------+------+

2 个答案:

答案 0 :(得分:0)

在没有看到实际表格的情况下,我写这样的请求有点困难,但我建议尝试这样的事情:

SELECT mentors.l_name, mentors.f_name, interns.l_name, PR1.Mark
FROM mentors

LEFT OUTER JOIN interns_specialty
ON mentors.mentor_id = interns_specialty.mentor_id

LEFT OUTER JOIN interns
ON interns.intern_id = interns_specialty.intern_id

LEFT OUTER JOIN Practice_result as PR1
ON PR1.intern_id = interns.intern_id

LEFT JOIN Practice_result as PR2
ON PR2.intern_id = interns.intern_id and PR2.Mark <> '5'

WHERE
PR2.intern_id is null
;

答案 1 :(得分:0)

谢谢大家,我决定搜索MIN(Mark)= 5 - 因为这意味着实习生没有更低的分数 - 这就是我所需要的。

然后使用建议谷歌的SQL关系部门&#39;在mysql中写这个。

做了以下事情:

SELECT mentors.l_name, mentors.f_name 
FROM mentors

LEFT OUTER JOIN interns_specialty
ON mentors.mentor_id = interns_specialty.mentor_id

LEFT OUTER JOIN interns
ON interns.intern_id = interns_specialty.intern_id

LEFT OUTER JOIN Practice_result
ON Practice_result.intern_id = interns.intern_id

GROUP BY mentors.l_name, mentors.f_name 

HAVING MIN(Practice_result.Mark) =5
;