Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.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
Sql查询以查找两个表之间是否缺少关系_Sql_Sql Server - Fatal编程技术网

Sql查询以查找两个表之间是否缺少关系

Sql查询以查找两个表之间是否缺少关系,sql,sql-server,Sql,Sql Server,我试图编写一个查询,发现表C和表a之间没有关系。唯一知道这种关系的表是表B |Table A| |Table B| |Table C| --------- --------- --------- |id: 1 | |id: 2, a_id: 1, c_id: 3| |id: 3 | |id: 4 | |id: 5 | 对于表

我试图编写一个查询,发现表C和表a之间没有关系。唯一知道这种关系的表是表B

|Table A|   |Table B|                     |Table C|
---------   ---------                     ---------
|id: 1  |   |id: 2, a_id: 1, c_id: 3|     |id: 3  |
|id: 4  |                                 |id: 5  |
对于表C中与表A无关的每个条目,我都想了解它

示例输出:

|Output|
--------
|c_id: 3, a_id: 4|
|c_id: 5, a_id: 1|
|c_id: 5, a_id: 4|
希望你能跟上。我一直在绞尽脑汁想这个问题,但我看不到解决方案。

在a和C之间进行交叉连接,使用not EXISTS子句排除B中的组合

选择C.id作为C\U id,选择A.id作为C\U id 来自C,A 如果不存在,则从B中选择*,其中B.a_id=a.id和B.c_id=c.id 因为您标记了sql server,所以还可以使用EXCEPT子句

从C、A中选择C.id作为C\U id,A.id作为A\U id 除了 从B中选择c_id、a_id 第一种方法适用于所有SQL数据库。第二种方法只适用于某些情况,例如:

除了适用于MS SQL Server、PostgreSQL、DB2和SQLite之外。 负号对甲骨文有效。 MySQL没有这个功能。 在a和C之间进行交叉连接,使用NOT EXISTS子句排除B中的组合

选择C.id作为C\U id,选择A.id作为C\U id 来自C,A 如果不存在,则从B中选择*,其中B.a_id=a.id和B.c_id=c.id 因为您标记了sql server,所以还可以使用EXCEPT子句

从C、A中选择C.id作为C\U id,A.id作为A\U id 除了 从B中选择c_id、a_id 第一种方法适用于所有SQL数据库。第二种方法只适用于某些情况,例如:

除了适用于MS SQL Server、PostgreSQL、DB2和SQLite之外。 负号对甲骨文有效。 MySQL没有这个功能。
尝试左连接并查找空值

select A.id, C.id
from B left outer join A on A.id= B.a_id left outer join C on C.id = B.c_id
where B.a_id is null or B.c_id is null

尝试左连接并查找空值

select A.id, C.id
from B left outer join A on A.id= B.a_id left outer join C on C.id = B.c_id
where B.a_id is null or B.c_id is null

这应该对你有用

SELECT c.id, a.id 
FROM c 
  JOIN a 
WHERE (SELECT id 
  FROM b 
  WHERE b.a_id = a.id AND 
    b.c_id = c.id) IS NULL
ORDER BY c.id, a.id;

这应该对你有用

SELECT c.id, a.id 
FROM c 
  JOIN a 
WHERE (SELECT id 
  FROM b 
  WHERE b.a_id = a.id AND 
    b.c_id = c.id) IS NULL
ORDER BY c.id, a.id;
试试这个

SELECT C.id AS c_id, A.id AS a_id
FROM C cross join A left outer join b on B.a_id = A.id AND B.c_id = C.id
WHERE b.id is null
试试这个

SELECT C.id AS c_id, A.id AS a_id
FROM C cross join A left outer join b on B.a_id = A.id AND B.c_id = C.id
WHERE b.id is null