![](/img/trans.png)
[英]Mysql select column from one table to match up with another table to pull id
[英]Alter MySQL table column to match id's from another table
考慮以下兩個MySQL表:
companies
-----------------------
id ticker
1 AA
2 AAPL
3 ABT
4 AEP
tweets
-----------------------
tweet_id query (etc...)
1 $AA
2 $AA
3 $AAPL
4 $ABT
5 $AA
6 $AEP
7 $AEP
表“公司”包含超過700種股票的股票代碼。 表“ tweets”包含數百萬條tweets。 通過在Twitter.com上查詢每個股票代碼(以“ $”開頭)來收集這些推文,因為這是Twitter談論某只股票時的慣例。 我現在想規范化tweets表,如下所示:
tweets
-----------------------
tweet_id query (etc...)
1 1
2 1
3 2
4 3
5 1
6 4
7 4
因此,現在,通過查詢ID為1(即AA)的代碼獲得了tweet_id為1的tweet(不再需要將“ $”存儲在數據庫中)。 現在我的問題是:有沒有一種方法可以使用一個sql查詢更新tweets表中的查詢列? 非常感謝您的幫助,因為如果不可能的話,我看到很多工作要做(也許使用PHP):)
您可以使用更新聯接。 像這樣:
UPDATE tweets t
JOIN companies c ON t.query = CONCAT('$', c.ticker)
SET t.query = c.id;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.