通过脚本更改字段和端口mysql表数据?

时间:2012-03-20 18:04:27

标签: mysql bash port

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


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

我想将数据(转储)从oldtable移植到newtable。一个问题是,早期的表格使用char(1)表示存储值'Y'或'N'。现在newtable将它作为int存储为1或0。

如何在移植数据之前修复此问题?我应该使用shell脚本进行此类修复吗?搬运? 任何示例脚本或提示:)

3 个答案:

答案 0 :(得分:5)

INSERT INTO newtable 
SELECT uid,IF(active='Y',1,0) as active FROM oldtable

应该做的伎俩

答案 1 :(得分:3)

INSERT INTO newtable (uid, active)
    SELECT uid, IF(active='Y', 1, 0) AS active
    FROM oldtable

此语法的文档:http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-select-into-table.html

答案 2 :(得分:0)

开玩笑版:

INSERT INTO newtable 
SELECT uid,FIELD(active,'Y') as active 
FROM oldtable