John
John

Reputation: 13719

MySQL copy two table columns to another table with different column names

I have two tables:

pages1

+---------------+-------------+
| id            | int         |
+---------------+-------------+
| content       | medium_text |
+---------------+-------------+
| date_modified | int         |
+---------------+-------------+

pages2

+---------+-------------+
| id      | int         |
+---------+-------------+
| content | medium_text |
+---------+-------------+
| date    | int         |
+---------+-------------+

I'm having trouble copying content and date_modified columns from the pages1 table to the content and date columns in the pages2 table.

I get Unknown column errors and I've attempted to use AS (alias) in case it was a mismatch though I'm 100% certain the columns exist. I've used the obnoxious ticks and the whole nine yards:

INSERT INTO pages2 (`content`, `date`) VALUES
(SELECT `content`, `date_modified` FROM `pages` WHERE `pages2`.`id` = `pages`.`id`);

Upvotes: 0

Views: 52

Answers (3)

Sadikhasan
Sadikhasan

Reputation: 18600

INSERT INTO pages2 (`content`, `date`) 
SELECT `content`, `date_modified` FROM `pages`

Upvotes: 1

Ankit Agrawal
Ankit Agrawal

Reputation: 2454

INSERT INTO pages2 (`content`, `date`) 
SELECT `content`, `date_modified` 
FROM `pages`
inner join pages2 on `pages`.`id` = `pages2`.`id`;

Upvotes: 0

Priyanshu
Priyanshu

Reputation: 881

Try this query:

INSERT INTO pages2 (`content`, `date`) 
SELECT `content`, `date_modified` FROM `pages` WHERE `pages2`.`id` = `pages`.`id`;

Upvotes: 1

Related Questions