【问题标题】:Is there a better way to write update table1 where x in (select x from table2 inner join table1)?有没有更好的方法来编写 update table1 where x in (select x from table2 inner join table1)?
【发布时间】:2023-02-13 20:17:31
【问题描述】:

我有一个sql更新语句:

update table1 set col1='val' where id in (select t2.id
                     from table2 t2
                              inner join table1 t1 on t1.id = t2.id
                     where someCondition);

有没有更好的写法?我想以某种方式不使用连接,因为我在更新构造中有表。

【问题讨论】:

    标签: sql sql-update


    【解决方案1】:

    也许用exists()

    UPDATE table1 s
    SET s.col1='val'
    WHERE EXISTS(SELECT 1 FROM table2 t
                 WHERE s.id = t.id AND someCondition)
    

    【讨论】:

    • 我已经更新了这个问题,我在想也许有一种方法可以使用更新构造中的 table1 来不使用 join with table1
    • 此查询不会再次加入 table1 @animos。在我看来正是你所要求的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-17
    相关资源
    最近更新 更多