【问题标题】:update using the resultset from CTE in H2使用 H2 中 CTE 的结果集进行更新
【发布时间】:2019-10-21 12:22:46
【问题描述】:

我正在尝试使用 CTE 的结果集更新表。参考this question

如果table_b是这样的CTE结果,查询是否有效

With table_b as (select a_id from table_x)
update table_a a
set a.b_id = (select b.id from table_b b where b.a_id = a.id)
where a.id = (select b.a_id from table_b b where b.a_id = a.id)

【问题讨论】:

    标签: sql-update h2 common-table-expression


    【解决方案1】:

    id也需要选择,否则b.id将找不到。

    with table_b as (select id, a_id from table_x)
    update table_a a
    set a.b_id = (select b.id from table_b b where b.a_id = a.id)
    where a.id = (select b.a_id from table_b b where b.a_id = a.id)
    

    您还应该考虑使用MERGE 命令而不是UPDATE,它可能更有效。

    with table_b as (select id, a_id from table_x)
    merge into table_a using table_b on table_a.id = table_b.a_id
    when matched then update set b_id = table_b.id;
    

    【讨论】:

      猜你喜欢
      • 2016-08-22
      • 2016-02-15
      • 1970-01-01
      • 2021-09-06
      • 2023-03-28
      • 2015-01-07
      • 1970-01-01
      • 2015-03-13
      • 2015-10-22
      相关资源
      最近更新 更多