select a value where it doesn't exist in another table

后端 未结 6 1538
忘掉有多难
忘掉有多难 2021-02-04 23:09

I have two tables

Table A:

ID
1
2
3
4

Table B:

ID
1
2
3

I have two requests:

  • I want to
6条回答
  •  野性不改
    2021-02-04 23:44

    SELECT ID
      FROM A
     WHERE ID NOT IN (
          SELECT ID
            FROM B);
    

    SELECT ID    
      FROM A a
     WHERE NOT EXISTS (
          SELECT 1 
            FROM B b
           WHERE b.ID = a.ID)
    

             SELECT a.ID 
               FROM A a    
    LEFT OUTER JOIN B b 
                 ON a.ID = b.ID    
              WHERE b.ID IS NULL
    

    DELETE 
      FROM A 
     WHERE ID NOT IN (
          SELECT ID 
            FROM B) 
    

提交回复
热议问题