Forum Moderators: open

Message Too Old, No Replies

Moving content from two MySQL tables

         

chabbs

12:46 pm on Oct 19, 2009 (gmt 0)

10+ Year Member



I'm trying to copy information from two tables and add them into one. I have successfully transferred this information using the code below via phpMyAdmin but the problem is the second part creates new rows and the titles and content are mismatched.

INSERT INTO destination-table (content)
SELECT table1 FROM content;

INSERT INTO destination-table (titles)
SELECT table2 FROM titles;

Here's my situation, I currently have an article directory and I want to move the articles over to a new database. The current database stores the titles of the articles in one table and the article body in another table. The new database stores titles and body in one table using separate columns. I want to move article body from table 1 and titles from table 2 into the new database.

This is what happens when using the above method.

Rows

1 content
2 content
3 content
4 titles
5 titles
6 titles

The information goes into the right columns but the titles and content don't match because the titles are placed in newly created rows. So lets say I have 50 articles, the content is found in row 1 - 50 and the titles are found in rows 51 - 100.

How can I successfully have titles and content in the same rows?

I'm new to MySQL databases. Thank you for your time and patiece. :)

rocknbil

4:44 pm on Oct 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's similar to the method suggested for re-ordering fields [dev.mysql.com] into a new table.

insert into new_table select columns-in-new-order from old_table;

so something like

insert into new_table (content,titles) values (select content.txt,titles.txt from content, titles where content.id=titles.content_id);

The previous probably has syntax errors, but that's the general approach.

If you can't work out a single statement, you'd have to do it with loops in programming, making sure the titles.content_id is associated with the corresponding content.id.