简体   繁体   English

更改MySQL表列以匹配另一个表的ID

[英]Alter MySQL table column to match id's from another table

Consider the following two MySQL tables: 考虑以下两个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

The table "companies" contains over 700 ticker symbols for various stocks. 表“公司”包含超过700种股票的股票代码。 The table "tweets" contains millions of tweets. 表“ tweets”包含数百万条tweets。 These tweets were gathered by querying Twitter.com for each ticker, prepended by "$", as this is the convention on Twitter when talking about a certain stock. 通过在Twitter.com上查询每个股票代码(以“ $”开头)来收集这些推文,因为这是Twitter谈论某只股票时的惯例。 I now would like to normalize the tweets table, as follows: 我现在想规范化tweets表,如下所示:

tweets
-----------------------
tweet_id query (etc...)
1        1
2        1
3        2
4        3
5        1
6        4
7        4

So now, the tweet with tweet_id one was obtained by querying for ticker with id 1, being AA (the "$" is not necessary to store in the database anymore). 因此,现在,通过查询ID为1(即AA)的代码获得了tweet_id为1的tweet(不再需要将“ $”存储在数据库中)。 Now my question would be: is there a way to update the query-column in the tweets-table with one sql-query? 现在我的问题是:有没有一种方法可以使用一个sql查询更新tweets表中的查询列? Your help would be greatly appreciated, since I see a lot of work ahead (by using PHP, perhaps) if this is not possible :) 非常感谢您的帮助,因为如果不可能的话,我看到很多工作要做(也许使用PHP):)

You can use an update join. 您可以使用更新联接。 Something like this: 像这样:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM