2010-10-27 38 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語句來更新新列的所有值。

相關問題