使用另一个表的另一列的数据更新列

时间:2012-07-04 09:57:13

标签: mysql sql

我有两张桌子

  1. titles_downloads
  2. title_history
  3. 目前我将titles_downloads中的idtitle_history保存为列。 title_history有一个列idtitle,它与idtitle_history不同。

    我想将titles_downloads表中的idtitle_history更改为idtitle

    title_history表的示例数据

           idtitle_history                 idtitle
                1                             160
                2                             210
                3                             345
    

    titles_downloads

           iddownloads                      idtitle_history
                1                              1
                2                              2
                3                              3
    

    我想将此表中的idtitle替换为160,210,345 ......

    由于

2 个答案:

答案 0 :(得分:1)

尝试

update titles_downloads
inner join title_history 
on title_history.idtitle_history  = titles_downloads.idtitle_history 
set idtitle_history = title_history.idtitle

答案 1 :(得分:0)

试试这个:

UPDATE titles_downloads td
JOIN title_history th ON td.iddownloads = th.iddownloads
SET th.iddownloads = td.idtitle
相关问题