Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL-其中一个表不是另一个表(多个字段)_Mysql_Sql - Fatal编程技术网

Mysql SQL-其中一个表不是另一个表(多个字段)

Mysql SQL-其中一个表不是另一个表(多个字段),mysql,sql,Mysql,Sql,我有下列表格 它们之间的关系如下:模包含所有模块,而模仅包含每个学生制作的模块。内部不标识学生 select modulos.codDisc, modulos.numero from modulos left join mods_alunos on modulos.codDisc=mods_alunos.codDisc and modulos.numero=mods_alunos.numero where (mods_alunos.codDisc

我有下列表格

它们之间的关系如下:模包含所有模块,而模仅包含每个学生制作的模块。内部不标识学生

    select modulos.codDisc, modulos.numero from modulos
    left join mods_alunos
    on modulos.codDisc=mods_alunos.codDisc
    and modulos.numero=mods_alunos.numero
    where 
    (mods_alunos.codDisc is null 
    and mods_alunos.numero is null
    )
此查询提供缺少的模块,但不考虑学生

我正在寻找一个查询,其中列出了某个学生缺少的模块

编辑1-样本数据

模数

codDisc | numero
------- | ------
1       | 1
1       | 2
1       | 3
2       | 1
2       | 2
莫德斯阿卢诺斯酒店

nInterno | codDisc  | numero
-------- | -------- | ------
11       | 1        | 1
11       | 1        | 2
11       | 1        | 3
11       | 2        | 1
12       | 2        | 2
在这个例子中,我想要的是一个查询,当我询问student 11缺少哪些模块时,它会给出codDisc 2,numero 2。UPDATE:`EXCEPT不受MySQL支持

除以下情况外,请简单使用:

不在将是一个选择,虽然稍微复杂一点,所以我会去,除非提到。


您可以这样做:

SELECT * FROM modulus WHERE codeDisc NOT IN(SELECT codeDisc  FROM mods_alunos WHERE nInterno=?) AND numero NOT IN (SELECT numero FROM mods_alunos WHERE nInterno=?

您将需要从学生表而不是链接表进行操作。试试这个:

select s.Id, m.codDisc, m.numero 
from student s
cross join modulos m
where not exists(select *
                 from mods_alunos ma 
                 where ma.codDisc = m.codDisc 
                 and ma.numero = m.numero
                 and ma.nInterno = s.Id)

其中student是您的student表,s.Id是您的student Id

您的问题不清楚……您能否添加一些示例数据和所需的输出以使其易于理解您一直尝试的是反连接。我不建议使用它,当一个简单的EXCEPT或NOT IN完成任务时。仅当您使用直接方法实际遇到性能问题时,才使用反联接。关于这个问题:你必须在ON子句中添加and mods_alunos.ninterno=123,才能找到缺少的模块Student 123。在我看来,mods_alunos是学生和模块之间的一个链接表,如果你想得到一个学生缺少的所有模块,你需要从Student master表中找到。请参阅下面的我的答案我只想知道codDisc丢失的位置我想知道当codDisc和numero组合丢失时尝试了except,它给了我一个错误[Err]1064-您的SQL语法中有一个错误;请查看与您的MySQL服务器版本对应的手册,以了解在第3Ah行的“select codDisc,numero from mods_alunos where nInterno=1”附近使用的正确语法,抱歉,MySQL不支持的标准功能之一除外。那么,不幸的是,在MySQL中,NOT IN是最简单的方法。您能解释一下为什么使用交叉连接而不是不同的连接吗?@Cacheira交叉连接获取每个学生的所有模块,然后在where子句中排除已经链接的模块。@Cacheira:查询为所有学生提供缺少的模块。因为你只想知道某个学生缺少哪些模块,所以你不需要这个。顺便问一下:TOP对MySQL有用吗?我想你必须在那里使用极限。无论如何,这是多余的,应该省略,以便让DBMS自己找到最佳执行计划。我对查询做了一些更改:将top 1更改为*并在where末尾添加了我需要的两个And,得到了我需要的结果
SELECT * FROM modulus WHERE codeDisc NOT IN(SELECT codeDisc  FROM mods_alunos WHERE nInterno=?) AND numero NOT IN (SELECT numero FROM mods_alunos WHERE nInterno=?
select s.Id, m.codDisc, m.numero 
from student s
cross join modulos m
where not exists(select *
                 from mods_alunos ma 
                 where ma.codDisc = m.codDisc 
                 and ma.numero = m.numero
                 and ma.nInterno = s.Id)