代码之家  ›  专栏  ›  技术社区  ›  Nick LaMarca

与内部联接查询相反

  •  32
  • Nick LaMarca  · 技术社区  · 14 年前

    Table 1
    
    2 columns: ID, Name
    
    Table 2
    
    2 columns: ID, Name
    

    我希望查询显示表1中不在表2中的名称。所以过滤掉表1中表2中的所有名称就是结果查询。使用ID进行筛选,而不是使用name。

    这将有助于我的努力。提前谢谢

    5 回复  |  直到 14 年前
        1
  •  59
  •   Andrew    14 年前
    Select * from table1
    left join table2 on table1.id = table2.id
    where table2.id is null
    
        2
  •  27
  •   Joe Stefanelli    14 年前

    这应该比 left join...is null 版本。见 here here

    select t1.id, t1.name
        from table1 t1
        where not exists(select null from table2 t2 where t2.id = t1.id)
    
        3
  •  17
  •   DForck42    14 年前

    使用此查询

    select
    t1.*
    from table1 t1
    left outer join table2 t2
    on t1.id=t2.id
    where t2.id is null
    

        4
  •  3
  •   i_saw_drones    10 年前
    SELECT Table1.ID, Table1.Name, Table2.ID 
    FROM Table1 LEFT OUTER JOIN Table2 ON Table1.ID = Table2.ID 
    WHERE Table2.ID IS NULL 
    

    我想应该是这样。

        5
  •  0
  •   Mojgan Mazouchi    7 年前
    SELECT * FROM table1
    WHERE table2.id NOT IN (SELECT id FROM table2)