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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里