Find records that do not have a corresponding record in the same table

前端 未结 4 1799
走了就别回头了
走了就别回头了 2021-01-29 00:57

I have the following table:

 ID | JobID | Data   | ResultType
---------------------------------
  1 | 12345 | XXXX   | 0
  2 | 12345 | YYYY   | 1
  3 | 23456 | A         


        
相关标签:
4条回答
  • 2021-01-29 01:28

    Something like

    select * from jobs where jobId not in 
              (select jobId from jobs where resultType = 1)
    
    0 讨论(0)
  • 2021-01-29 01:34
    SELECT JobID FROM <TableName> WHERE ResultType <> 1
    

    Is this what you want ???

    0 讨论(0)
  • 2021-01-29 01:47

    Another way (not tested):

    SELECT JobId FROM Jobs GROUP BY jobId HAVING max(ResultType) = 0
    
    0 讨论(0)
  • 2021-01-29 01:47

    Use this query:

    SELECT JobID 
    From table1 a 
    WHERE NOT EXISTS
     (SELECT 1 FROM table1 b WHERE b.JobID = a.JobID AND b.ResultType = 1)
    
    0 讨论(0)
提交回复
热议问题