MySQL: Re-doing my structures. How can I move my existing data?
I decided to redo my MySQL table structure. How can I move the data into the new tables from old tables. What would be the be开发者_StackOverflowst way to do it? Currently, I am using phpmyadmin. Is there a way to pipe like in Unix i.e. SELECT * FROM table | UPDATE
?
INSERT...SELECT
is your best bet. Reference: http://dev.mysql.com/doc/refman/5.5/en/insert-select.html
eg:
INSERT INTO destinationTable (col1, col2, col3)
SELECT oldcol1, oldcol2, oldcol3
FROM sourceTable
You can move data into the new tables by executing
INSERT newtable SELECT * FROM oldtable
if the new tables aren't created yet:
CREATE TABLE newtable
SELECT * FROM oldtable
You don't have to, simply do something like
create table new_table like old_table;
insert into new_table select * from old_table;
If the structure is different:
create table new_table ...; <-- whatever definition
insert into new_table
select
(FUNCTION_CALL to change column value)
from old_table;
example
insert into new_table
select
concat(title, ' by ', author) as title ...
from old_table;
精彩评论