当它们没有任何共同点时,如何从表 2 更新表 1? [英] How to update Table 1 from Table 2 when they have nothing in common?

查看:22
本文介绍了当它们没有任何共同点时,如何从表 2 更新表 1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个表 - 每个表只有一行.我想用 Table2 中的相应列更新 Table1 上的 o1 和 o2 列.

I have 2 tables - each has a single row only. I want to update o1 and o2 columns on Table1 with the corresponding columns in Table2.

create table Table1(c1 int, c2 int, o1 int, o2 int)
create table Table2(o1 int, o2 int)

我有以下内容,这很可怕(但有效).

I have the following, which is horrible (but works).

update Table1
set o1 = (select o1 from Table2),
    o2 = (select o2 from Table2)

有没有更好的方法?

推荐答案

您可以使用:

update table1
    set o1 = t2.o1,
        o2 = t2.o2
from table2 t2;

这篇关于当它们没有任何共同点时,如何从表 2 更新表 1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆