dongpa9277 2011-10-28 12:50
浏览 11
已采纳

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

Consider the following two MySQL tables:

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. The table "tweets" contains millions of 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. I now would like to normalize the tweets table, as follows:

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). Now my question would be: is there a way to update the query-column in the tweets-table with one sql-query? Your help would be greatly appreciated, since I see a lot of work ahead (by using PHP, perhaps) if this is not possible :)

  • 写回答

1条回答 默认 最新

  • duandou1903 2011-10-28 12:53
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥15 ensp路由器启动不了一直报#
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了