简体   繁体   English

如何从B表的所有相应行中仅选择A的值都为X的条目?

[英]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? 如何只选择所有实习生的Practice_result.Mark等于5的mentors.l_name,mentors.f_name?

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'. 或者,我可以选择所有带有Practice_result.Mark ='5'的ROWS,而不会排除那些也拥有使用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. 而且我需要排除那些<> 5的对象,因此在这种情况下只返回Mentor1,因为只有他的“实习生”只有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. 我尝试将ORDER BY与LIMIT 1结合使用,但仍然不知道如何使它适用于较小的一组相同mentor.l_name,但适用于所有实习生。

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: 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. 谢谢大家,我决定搜索MIN(Mark)= 5-因为这意味着实习生没有较低的分数-这正是我所需要的。

And then used a suggestion to google 'sql relational division' to write this in mysql. 然后用一个建议谷歌“ SQL关系除法”写这个在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
;

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

相关问题 如何选择表a中具有表b中给出的n个特征的所有行 - How can I select all rows in a table a, which have n characteristics given in a table b 从表B获取所有与表A的多个条目(给定列表)相关的条目 - Get all entries from Table B which have a relation to multiple entries (given list) from Table A 如何获得表A所有数据与表B只有与表A匹配的行 - How to get Table A all data with tables B only rows which matches with Table A 选择语句,只有在第二个表中设置为true的行 - select statement with only rows which have set true in second table 如何只从表中选择行一个clumns值超过MySQL中的b表计数值 - How to select only rows from a table one clumns value is more than b table count value in MySQL 在1个字段上只选择1行,有2行 - select only 1 rows on 1 field which have 2 rows MySQL:选择仅包含两行并具有特定值的所有行吗? - MySQL: Select all that only have two rows, with specific values? 如何仅选择具有多个具有值的给定字段的行 - How to select only those rows which have more than one of given fields with values 仅当与相同ID配对的所有行都有日期时,才从表中选择字段 - Select field from table only when all rows paired to same ID have a date 如何选择在另一个表中具有全部或没有相应值的行? - How to select rows, which have all or none corresponding values in another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM