在 SQL 服务器中逐行比较来自两个不同表的两个文本列
Compare two text columns from two different tables row wise in SQL server
我有两个 table,如下所示:
现在我希望第一个 table 的 amt 列填充到第二个 table 中,因为第二个 table 的所有列都是第一个 [=26] 的子集=].
为澄清起见,在上面的示例中,第一行将被填充,因为 table 2 的所有列都是 table 1 的子集,但最后 'c4' 与table 1 的元素(同一行),因此该行 'amt' 列将为空白。
我需要解决它 sql (Microsoft SQL server 2014)
任何线索将不胜感激
我想这就是你想要的:
select t2.*, t1.amt
from table2 t2 left join
table1 t1
on (t2.a = t1.a or t2.a is null) and
(t2.b = t1.b or t2.b is null) and
(t2.c = t1.c or t2.c is null) and
(t2.d = t1.d or t2.d is null);
您可以轻松地将其转换为更新:
update t2
set amt = t1.amt
from table2 t2 left join
table1 t1
on (t2.a = t1.a or t2.a is null) and
(t2.b = t1.b or t2.b is null) and
(t2.c = t1.c or t2.c is null) and
(t2.d = t1.d or t2.d is null);
我有两个 table,如下所示:
现在我希望第一个 table 的 amt 列填充到第二个 table 中,因为第二个 table 的所有列都是第一个 [=26] 的子集=]. 为澄清起见,在上面的示例中,第一行将被填充,因为 table 2 的所有列都是 table 1 的子集,但最后 'c4' 与table 1 的元素(同一行),因此该行 'amt' 列将为空白。
我需要解决它 sql (Microsoft SQL server 2014)
任何线索将不胜感激
我想这就是你想要的:
select t2.*, t1.amt
from table2 t2 left join
table1 t1
on (t2.a = t1.a or t2.a is null) and
(t2.b = t1.b or t2.b is null) and
(t2.c = t1.c or t2.c is null) and
(t2.d = t1.d or t2.d is null);
您可以轻松地将其转换为更新:
update t2
set amt = t1.amt
from table2 t2 left join
table1 t1
on (t2.a = t1.a or t2.a is null) and
(t2.b = t1.b or t2.b is null) and
(t2.c = t1.c or t2.c is null) and
(t2.d = t1.d or t2.d is null);