简体   繁体   English

当它们没有共同点时,如何从多个其他表中更新表 1?

[英]How to update Table 1 from multiple other tables when they have nothing in common?

I have a main table and 3 other tables.我有一个主表和 3 个其他表。 Each of these tables has a single row only.这些表中的每一个都只有一行。 I would like to update columns of TableMain with corresponding columns from other tables.我想用其他表中的相应列更新 TableMain 的列。 And these tables don't have any columns that I can join.而且这些表没有我可以加入的任何列。

create table TableMain(a1 int, a2 int, b1 int, b2 int, c1 int, c2 int)
create table TableA(a1 int, a2 int)
create table TableB(b1 int, b2 int)
create table TableC(c1 int, c2 int)

I have the following, which works, but looks horrible:我有以下内容,它有效,但看起来很糟糕:

update TableMain
set a1 = (select a1 from TableA),
    a2 = (select a2 from TableA),
    b1 = (select b1 from TableB),
    b2 = (select b2 from TableB),
    c1 = (select c1 from TableC),
    c2 = (select c2 from TableC)

I've tried the code below but I am getting NULLs, so this approach doesn't work:我已经尝试了下面的代码,但我得到了 NULL,所以这种方法不起作用:

update TableMain
set a1 = ta.a1, a2 = ta.a2, 
    b1 = tb.b1, b2 = tb.b2, 
    c1 = tc.c1, c2 = tc.c2, 
from TableA ta, TableB tb, TableC tc

Is there a more elegant way to update these columns?有没有更优雅的方式来更新这些列?

PS I asked a similar question , but it addressed updating from a single table. PS 我问了一个类似的问题,但它解决了从单个表进行更新的问题。

Consider:考虑:

UPDATE tm
SET tm.a1 = ta.a1,
    tm.a2 = ta.a2, 
    tm.b1 = tb.b1, 
    tm.b2 = tb.b2, 
    tm.c1 = tc.c1, 
    tm.c2 = tc.c2
FROM TableMain tm
CROSS JOIN TableA ta
CROSS JOIN TableB tb
CROSS JOIN TableC tc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM