MySQL 中创建了新表 tbl_mgm_test1,表中部分列数据来源于旧表 tbl_mgm_test 中的部分属性列,要实现旧表部分列数据迁移到新表指定列,可按照如下语句实现。
查看当前 mysql 的所有数据库:
mysql> select * from INFORMATION_SCHEMA.SCHEMATA;
--或者
mysql> show databases;
查看当前 mysql 的指定库的指定表的所有列:
mysql> select * from INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA='testmgmdb' and TABLE_NAME='tbl_mgm_test';
--或者
mysql> SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='testmgmdb' AND TABLE_NAME='tbl_mgm_test';
--或者
mysql> use testmgmdb;
Database changed
mysql> show full columns from tbl_mgm_test;
将 tbl_mgm_test 表部分列数据迁移到 tbl_mgm_test1 表指定列:
mysql> INSERT INTO tbl_mgm_test1 (test_id, test_nm, test_url) SELECT test_id, test_name, test_url FROM tbl_mgm_test WHERE test_tp = "M";
tbl_mgm_sp 表存储了完整的 Sp 信息,将表 tbl_mgm_test1 中的 sp_nm 属性更新为 tbl_mgm_sp 表中的 sp_nm 值,以两表 sp_id 做等值关联:
mysql> UPDATE tbl_mgm_test1 a INNER JOIN tbl_mgm_sp b ON a.sp_id = b.sp_id SET a.sp_nm = b.sp_nm;