代码之家  ›  专栏  ›  技术社区  ›  Moe Sisko

删除行-使用join vs in,当在join中返回重复行时

  •  1
  • Moe Sisko  · 技术社区  · 6 年前

    示例代码:

    create table t1 (id int not null primary key, name varchar(10))    
    insert t1 values (1, 'one')
    insert t1 values (2, 'two')
    insert t1 values (3, 'three')    
    
    create table t2 (id2 int not null)    
    insert t2 values (2)
    insert t2 values (2)
    go
    
    select * from t1 join t2 on t1.id = t2.id2  -- S1: returns 2 rows
    select * from t1 where t1.id in (select t2.id2 from t2)  -- S2: returns 1 row
    go
    -- only run D1 or D2, not both
    delete t1 from t1 join t2 on t1.id = t2.id2  -- D1
    delete t1 from t1 where t1.id in (select t2.id2 from t2)  -- D2
    

    标记为:S1的行按预期返回多行,而S2只返回1行,因为使用了“IN”。

    现在,删除时,D1和D2似乎都起作用(即未报告错误)。 对于D1,SQL server似乎在删除(?)之前在id上做了一些不同的操作。 我的问题是,在这种情况下,D1和D2在功能上是等价的吗。

    0 回复  |  直到 6 年前