查询没有指向它们的外键(NOT NULL外键)的行

时间:2015-04-30 15:47:25

标签: sql sql-server sql-server-2008

我已经阅读了几篇描述与我的问题非常类似的帖子,但我还没有能够解决我的问题。就是这样:

有表A,B,C和D.表D由其他3个表引用,只有表(A)具有可空的'外键和我需要找到的是D中没有被其他3个表指向/引用的行。到目前为止,我已经能够使用此代码过滤所有行,包括我想要查找的行:

SELECT D.Id as Id, A.Id as A_Id, B.Id as B_Id, C.Id as C_Id
FROM D
LEFT OUTER JOIN A --this is the nullable one
ON D.Id = A.D_Id
LEFT OUTER JOIN B
ON D.Id = B.D_Id
LEFT OUTER JOIN C
ON D.Id = C.D_Id

虽然通过查看结果我可以在结果中看到我需要的行,如果我尝试按IS NULL过滤,它们会将外键字段显示为null,那么我根本没有得到任何结果是我使用的查询:

SELECT D.Id as Id, A.Id as A_Id, B.Id as B_Id, C.Id as C_Id
FROM D
LEFT OUTER JOIN A --this is the nullable one
ON D.Id = A.D_Id
LEFT OUTER JOIN B
ON D.Id = B.D_Id
LEFT OUTER JOIN C
ON D.Id = C.D_Id
WHERE A.D_Id IS NULL AND B.D_Id IS NULL AND C.D_Id IS NULL

请大家多谢。 提前致谢

修改

我试过这个并且虽然这是一个非常不同的方法但它有效:

SELECT *
FROM D
WHERE     Id not in (Select D_Id from A) 
      and Id not in (Select D_Id from B) 
      and Id not in (Select D_Id from C) 

3 个答案:

答案 0 :(得分:2)

第二个查询几乎正确!

或者代替

WHERE A.D_Id IS NULL AND B.D_Id IS NULL AND C.D_Id IS NULL

我会用

WHERE A.Id IS NULL AND B.Id IS NULL AND C.Id IS NULL

SELECT D.Id as Id, A.Id as A_Id, B.Id as B_Id, C.Id as C_Id
FROM D
   LEFT OUTER JOIN A --this is the nullable one
      ON D.Id = A.D_Id
   LEFT OUTER JOIN B
      ON D.Id = B.D_Id
   LEFT OUTER JOIN C
      ON D.Id = C.D_Id
WHERE A.Id IS NULL AND B.Id IS NULL AND C.Id IS NULL

Sql Fiddle Updated

答案 1 :(得分:1)

您是否尝试使用OR而不是AND来获取null以获得您想要的内容?可能是它在其中一个表的连接中为null,而在另一个表中不为null。

<script language="JavaScript">
lives=3
score=0

 document.write("Lives:",lives,"<br>");

 document.write("Score:",score);

 function refreshGame(){
   for(var i = 1 ; i<=3 ; i++){
      document.getElementById('pot'+i).src="imageedit_2_3920956067.gif";
   }
 }
function clickMeIDFunction(theElementID){
        var theElement = document.getElementById(theElementID);
        theElement.src = "nope.gif";
        //Sleep here so that you can see the changes:
        setTimeout(refreshGame,1000);
        //SetTimeOut would call refresh game after 1 seconde.
 }
 function clickMeIDFunction2(theElementID){
        var theElement = document.getElementById(theElementID);
        theElement.src = 'nope.gif';
        //Sleep here so that you can see the changes:
        setTimeout(refreshGame,1000);
        //SetTimeOut would call refresh game after 1 seconde.
 }
 function clickMeIDFunction3(theElementID){
        var theElement = document.getElementById(theElementID);
        theElement.src = "gold-bar-icon.png";
        //Sleep here so that you can see the changes:
        setTimeout(refreshGame,1000);
        //SetTimeOut would call refresh game after 1 seconde.
 }

 if (lives=0){
     document.write("Game over!");
 }

</script>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><img id="pot1" src="imageedit_2_3920956067.gif" width="238" height="170" class="pot" onclick="clickMeIDFunction('pot1')">
<img id="pot2" src="imageedit_2_3920956067.gif" width="238" height="170" class="pot" onclick="clickMeIDFunction2('pot2')">
<img id="pot3" src="imageedit_2_3920956067.gif" width="238" height="170" class="pot" onclick="clickMeIDFunction3('pot3')">
</center>
</div>`enter code here`

编辑:这能为您提供所需的结果吗?

SELECT D.Id as Id, A.Id as A_Id, B.Id as B_Id, C.Id as C_Id
FROM D
LEFT OUTER JOIN A --this is the nullable one
ON D.Id = A.D_Id
LEFT OUTER JOIN B
ON D.Id = B.D_Id
LEFT OUTER JOIN C
ON D.Id = C.D_Id
WHERE A.D_Id IS NULL OR B.D_Id IS NULL OR C.D_Id IS NULL

答案 2 :(得分:0)

您是否尝试使用NOT EXISTS排除其他表引用的表D条目?

SELECT 
    D.Id as Id
FROM 
    D
WHERE
    NOT EXISTS( SELECT 1 FROM A WHERE D.Id = A.D_Id)
    AND NOT EXISTS( SELECT 1 FROM B WHERE D.Id = B.D_Id)
    AND NOT EXISTS( SELECT 1 FROM C WHERE  D.Id = C.D_Id);