2012-03-20 23 views
2
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  |    | 
+------------+--------------+------+-----+---------+----------------+ 

我想从旧表中移植数据(转储)到newtable。一个问题是,表早些时候使用char(1)作为活动,它存储值'Y'或'N'。现在新表将其存储为int或1或0.通过脚本更改字段和端口mysql表数据?

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

回答

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

应该做的伎俩

+0

感谢它的工作。 – 2012-03-21 06:43:37

0

戏说版:

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