Hisham Alghamdi Hisham Alghamdi - 2 years ago 56
SQL Question

copy data from table to another without matching the primary number

I have two tables:

Table1 has the following columns (id, username, email).

Table2 has the following columns (id, name, username, email, password, registerDate, activation)

How can I copy all the data from Table1 to Table2?

Note: the id numbers of each row in Table1 NOT matching the id numbers in Table2. I just need to move the data from Table1 to Table2 as the rest columns in Table2 not necessary for me.

Answer Source

Single INSERT...SELECT can do that:

INSERT INTO table2 (`username`, `email`) (SELECT `username`, `email` FROM table1)

This will append all records from table1 into table2.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download