问题
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)
Is there a better way?
回答1:
You can use:
update table1
set o1 = t2.o1,
o2 = t2.o2
from table2 t2;
回答2:
By this way, you can do as expected, even if there were 1 or 'n' rows-
UPDATE T1
SET T1.o1 = T2.o1,
T1.o2 = T2.o2
from table1 T1 INNER JOIN table2 T2 ON T1.o1=T2.o1 AND T1.o2=T2.o2;
来源:https://stackoverflow.com/questions/58206904/how-to-update-table-1-from-table-2-when-they-have-nothing-in-common