选择表中不具有可为空外键的另一行中的行

时间:2012-10-04 15:34:32

标签: sql database sqlite select android-sqlite

我有以下数据库可以在Android 3.0平板电脑应用程序中使用它。

enter image description here
我想选择EReportDefect上不存在的每个缺陷。正如您所见,EReportDefect.defectId 可能为空

此选择返回0行。但是有些缺陷不在EReportDefect表格上。

SELECT 
  Defect.defectId,
  Defect.description
FROM 
  Defect 
WHERE 
   qapId = ? AND
   defectId NOT IN 
     (SELECT defectId FROM EReportDefect WHERE eReportId = ?);

我做错了什么?

3 个答案:

答案 0 :(得分:3)

您可以使用defectID

加入LEFT JOIN列上的两个表格
SELECT  a.*
FROM    Defect a
        LEFT JOIN EReportDefect b
            ON a.defectID = b.defectID
WHERE   b.defectID IS NULL 
如果b.defectID上没有匹配项,则

NULL会有a.defectID个值。

更新1

SELECT a.*
FROM    Defect a
        LEFT JOIN EReportDefect b
            ON a.defectID = b.defectID
WHERE  b.defectID IS NULL AND
        a.qapID = ? 
       -- AND b.ReportID = ?

答案 1 :(得分:0)

这就是我的作品:

SELECT
   Defect.defectId,
   Defect.description
FROM
   Defect
WHERE
   qapId = ? AND
   defectId NOT IN 
     (SELECT
        defectId
      FROM
        EReportDefect
      WHERE eReportId = ? AND defectId IS NOT NULL);

答案 2 :(得分:-1)

也许我错过了一些东西,但对我来说,似乎你删除了“qapId =?AND”和“WHERE eReportId =?”它应该只返回EReportDefect表中没有条目的结果

SELECT 
  Defect.defectId,
  Defect.description
FROM 
  Defect 
WHERE 
   defectId NOT IN 
     (SELECT defectId FROM EReportDefect);