简体   繁体   English

mysql左外连接

[英]mysql left outer join

i have some sql table 我有一些SQL表

table classes(id_classes,specialty,level)
table Teaching unit(id_unit,name,...,#id_classes)
table courses(#id_course,....,#id_unit)
table groupes(id_groupe,...,#id_classes)
table assignment(#id_groupe,#id_course)

i want to find courses which not in assignment for a given groupe so this courses are depending to unit that depend to the groupe's classe 我想找到不属于给定组的课程,因此该课程取决于取决于组的班级的单元

i wrote this code 我写了这段代码

select * from courses c
left join assignment a
on a.idcourse=c.idcourse and a.id_groupe ='given  id'
where a.id_courses is null

but this return the courses depend to all classes that not in assignment 但这返回的课程取决于所有未分配的课程

Additional Info (copied from comment) 附加信息(从评论中复制)

for exempl i have a classe with id 1 and classe with id 2 groupe with id 1 courses 1 ,2 and 3 depend to unit 1 thats depent to classe 1 courses 4,5 and 6 depend to unit 2 that depend to classe 2 i have a groupe 1 depend to classe 1 in the table asignement i have 2 rows (groupe1,courses1),(groupe1,courses2) i want to make a query that return the courses 3 that still not assigned to the groupe 1 the query return courses 3,4,5,and 6 but i want to get only the courses 3 例如,我有一个ID为1的班级和ID为2的班级,ID为1的班级,课程1,2和3依赖于单元1,而延后到课程1的课程4,5和6取决于依赖于课程2的单元2组1依赖表分配中的类1我有2行(groupe1,courses1),(groupe1,courses2)我要查询返回仍未分配给组1的课程3的查询,查询返回课程3 ,4、5和6,但我只想获得课程3

select c.* from courses c ,unit u where  (courses not in asignment where id_groupe=given id) and u.idunit=c.idunit and u.classes is the same classes form the given groupe

Base on your description your left join is not incorrect is that you didn't have any code that expresses your extra contraints "u.idunit=c.idunit and u.classes is the same classes form the given groupe" 根据您的描述,您的左联接是不正确的,因为您没有任何代码来表达您的额外矛盾“ u.idunit = c.idunit和u.classes是与给定组相同的类”

This should be the code you want: 这应该是您想要的代码:

SELECT *
FROM courses c
LEFT JOIN assignment a ON c.id_course = a.id_course
INNER JOIN teachingUnit u ON u.id_unit = c.id_unit 
                            AND u.id_classes IN (SELECT id_classes
                            FROM groupes
                            WHERE id_groupe =  ‘givenID’)
WHERE a.id_course IS NULL AND a.id_groupe = ‘givenID’   

this sub query 这个子查询

 (SELECT id_classes
   FROM groupes
   WHERE id_groupe =  ‘givenID’)

handles the constraint of u.classes is the same classes form the given groupe. 处理u.classes的约束是与给定组相同的类。 The INNER JOIN on teachingUnit u ON u.id_unit = c.id_unit handles the constraint of "and u.idunit=c.idunit" teachingUnit u ON u.id_unit = c.id_unit上的INNER JOIN teachingUnit u ON u.id_unit = c.id_unit处理“和u.idunit = c.idunit”的约束

I hope this helps. 我希望这有帮助。 Because in your example your table name and columns are inconsistent and your objective is hard to understand. 因为在您的示例中,表名和列不一致,并且目标难以理解。 This solution is the best I can come up with minimal assumptions that were made out of neccesity. 这个解决方案是我最好的解决方案,它是出于必要而做出的最小假设。

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

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