简体   繁体   中英

How to select only entries from A which have only values=X in all respective rows in B table?

How can I select only those mentors.l_name, mentors.f_name which have ALL their interns' Practice_result.Mark equal to 5?

Currently I can select only all of the marks of all 'interns' for all 'mentors' using the below:

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
;

The result is as follows:

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

Or I can select all ROWS with Practice_result.Mark = '5' without EXCLUDING those mentors who also have interns with Practice_result.Mark <> '5'.

And I need to exclude those with <> 5, so only Mentor1 to be returned in this case as only his 'interns' have only 5s.

I tried to use ORDER BY with LIMIT 1 but still don't get how to make it work for the smaller set of same mentor.l_name, but all of interns.

Tables: Mentors:

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

Interns:

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

It's a bit difficult for me to write such request without seeing actual tables, but I would recommend to try something like this:

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
;

Thanks everyone, I came to decision to search for MIN(Mark) = 5 - as it means that the interns do not have lower marks - that was what I needed.

And then used a suggestion to google 'sql relational division' to write this in mysql.

Did the following:

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
;

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