mysql> desc oldtable;
 +---------------+--------------+------+-----+---------+----------------+
 | Field         | Type         | Null | Key | Default | Extra          |
 +---------------+--------------+------+-----+---------+----------------+
 | uid           | int(11)      | NO   | PRI | NULL    | auto_increment |
 | active        | char(1)      | NO   |     | NULL    |                |
 | field3        | char(256)    | NO   |     | NULL    |                |
 | field4        | char(256)    | NO   |     | NULL    |                |
 +---------------+--------------+------+-----+---------+----------------+


 mysql> desc newtable;
 +------------+--------------+------+-----+---------+----------------+
 | Field      | Type         | Null | Key | Default | Extra          |
 +------------+--------------+------+-----+---------+----------------+
 | uid        | int(11)      | NO   | PRI | NULL    | auto_increment |
 | active     | tinyint(1)   | NO   |     | 0       |                |
 | field5     | int(12)      | NO   |     | 0       |                |
 | field6     | varchar(12)  | NO   |     | 0       |                |
 | field7     | varchar(12)  | NO   |     | 0       |                |
 +------------+--------------+------+-----+---------+----------------+

这与我之前的查询类似change a field and port mysql table data via script ?
[我想将数据(转储)从oldtable移植到newtable。一个问题是,前面的表对active使用char(1),它存储值“Y”或“N”。现在newtable将其存储为int 1或0。
如何在移植数据之前修复此问题?我应该使用shell脚本来修复和移植吗?任何示例脚本或提示:)]
但这个问题是,如果两个表的字段数不同,如何实现相同的移植
像上面那样?

最佳答案

答案与普雷维乌斯的问题答案相似:

INSERT INTO newtable (uid, active, field5, field6, field7 )
SELECT uid,FIELD(active,'Y') as active, 0,'',''
FROM oldtable

然后用新字段值更新newTable:
update newTable
set
  field5 = (select someExpression from someTable5 t where t.uid=newTable.uid),
  field6 = (select someExpression from someTable6 t where ...),
  field7 = (select someExpression from someTable7 t where ...)

此外,还可以将新字段定义为允许为空,并使此字段不带值。

关于mysql - 当两个表具有不同的字段时如何将数据移植到表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10379729/

10-11 01:21
查看更多