2010-10-27 69 views
1

表aggregate_monthly_conversations由列的用户_A,用户_B,user_B_location和表monthly_statistics仅由用户_A和用户_B如何添加到表中其他列与属于价值

我想列user_B_location添加到表monthly_statistics和用适当的值填充它。

为了得到合适的值user_B_location为用户_B表monthly_statistics我可以运行下面的查询:

SELECT t1.user_B_location 
FROM aggregate_monthly_conversations AS t1 
INNER JOIN monthly_statistics AS t2 ON t1.user_B = t2.user_B 

无论如何,我不知道如何将其他列添加到monthly_statistics与查询返回的值填充以上。如果有人能够帮助撰写解决这个问题的查询,我将不胜感激。

谢谢!

+0

您正在使用什么数据库? – 2010-10-27 13:18:05

回答

1

您需要先添加新列。添加完成后,可以使用所需的值更新它。

步骤1

alter table monthly_statistics 
    add user_B_location int /* or whatever datatype is appropriate */ 

步骤2

update ms 
    set user_B_location = amc.user_B_location 
    from monthly_statistics ms 
     inner join aggregate_monthly_conversations amc 
      on ms.user_B = amc.user_B 
0

您需要两个表之间的某种关系,然后只需编写一个Update语句来更新新列的所有值。

相关问题