2014-11-23 110 views
0

我有类似的表合并两列值从另一个表

enter image description here

如何合并这两个表?还有其他栏目,但这些都是一样的。

我该如何填充Table1与Table2的值或合并这些表? 在表2中只有1个客户。

因此,结果表将使所有客户都具有它们的值(表1将具有Customer4,销售额为50)。

谢谢。

+0

codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html – Mihai 2014-11-23 18:00:05

回答

1

要更新表做

update t1 
set t1.sales = t2.sales 
from table1 t1 
join table2 t2 on t1.customername = t2.customername 

,并作为选择使用

select t1.customername, 
     coalesce(t1.sales, t2.sales) as sales, 
     t1.date, 
     t1.variable1 
from table1 t1 
left join table2 t2 on t1.customername = t2.customername 
+0

这将做到这一点,谢谢。但是也可以在select中做到这一点?一些IFNULL与条件SubQuery我不知道? :_) – Muflix 2014-11-23 18:07:32

+0

我更新了答案 – 2014-11-23 18:36:57

+0

谢谢,那就是:-)) – Muflix 2014-11-25 19:40:09