Forum Moderators: open

Message Too Old, No Replies

Merge Tables Command

         

matthewamzn

6:36 pm on Mar 13, 2006 (gmt 0)

10+ Year Member



I want to merge two mysql tables, which are in the same database. What would the mysql query be?

Demaestro

9:47 pm on Mar 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What do you mean by merge? You mean you have a table with fields A, B, C and you have one with 1, 2, 3, and you want to make one table that has A, B, C, 1, 2, 3?

matthewamzn

10:11 pm on Mar 13, 2006 (gmt 0)

10+ Year Member



That's right, just combine the two tables together. Put the contents of one into the other.

Demaestro

10:40 pm on Mar 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You will need more then one SQL, this is rough but it will look something like this.

alter table A add column ONE datatype;

alter table A add column TWO datatype;

alter table A add column THREE datatype;

SELECT
ONE,
TWO,
THREE
INTO
A
FROM
B
where
A.primary_key = B.primary_key

If this isn't clear I can try to simplify, but basically you need to alter one table and add the columns from the other table.

Then to populate the new fields on the table you use a "Select into" statement. This statement will grab data from one table and insertI added a where clause that will look for matching unique ids I am assuming you have corrolating ids that can match the records up.