简体   繁体   English

mysql:从备份中恢复列的值

[英]mysql: restore a value of a column from backup

I'm having trouble.我有麻烦了。 Instead of launchin script而不是启动脚本

UPDATE table_name SET field=value WHERE id=12345

I launched我推出

UPDATE table_name SET field=value

Database is backuped every day (with mysqldump).每天备份数据库(使用 mysqldump)。 What is the simpliest way to restore the value of that column using the backup.使用备份恢复该列值的最简单方法是什么。 obviously I cannot apply that backup dirrectly as database is continiously changed.显然我不能直接应用该备份,因为数据库是不断变化的。

Thank you in advance!!先感谢您!!

I would create a new table 'table_name2' identical to your 'table_name' but containing the data of your backup.我会创建一个新表 'table_name2' 与您的 'table_name' 相同,但包含您的备份数据。

Then use this query:然后使用这个查询:

UPDATE table_name SET
table_name.field = (SELECT table_name2.field 
                    FROM table_name2 
                    WHERE table_name.id = table_name2.id)

So you wiped out all values in the column and want to put all values back in the correct rows?因此,您清除了列中的所有值并希望将所有值放回正确的行中?

I would recommend unzipping the backup into a separate database and doing something like我建议将备份解压缩到一个单独的数据库中并执行类似的操作

    UPDATE live_db.table_name live 
INNER JOIN backup_db.table_name backup
        ON live.id = backup.id 
       SET live.field = backup.field

I would also recommend making a duplicate of your live site to try this on first ;)我还建议您复制您的实时站点以先尝试此操作;)

I would restore the backup of the table in another table called table_name_bck, then run the following sql.我将在另一个名为 table_name_bck 的表中恢复该表的备份,然后运行以下 sql。

UPDATE table_name_bck tb, table_name t
SET t.field = tb.field
WHERE tb.id = t.id

Of course try this in a test environment before to prevent worsening your situation.当然,请先在测试环境中尝试此操作,以防止您的情况恶化。

Source: http://www.electrictoolbox.com/article/mysql/cross-table-update/来源: http : //www.electrictoolbox.com/article/mysql/cross-table-update/

您可以尝试将备份加载到另一个数据库中,然后进行类似的操作(假设 db1 是生产数据库,而 db2 是临时数据库)

update db1.table set db1.field = db2.field where db1.id = db2.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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