QuadB
QuadB

Reputation: 55

why won't INSERT INTO SELECT work in phpmyadmin

I have two tables in the same DB. Table1 was autogenerated with a csv import, column names are all like: COL 1, COL 2, etc. Table2 is a table I created. In both tables, all fields are VARCHAR NULL nullable, except for a PK AI field in Table2. Table 1 has 18 fields. Table 2 has 21 fields.

I am trying to insert the first 18 fields from table1 into table2. My code looks like this.

INSERT INTO table2
(First, Last, Address...) 
SELECT COL 1, COl 2, COl 3
FROM table1;

This is not working. I have tried with and without backtics and single quotes on both field lists. The error message only tells me that the error is on line 3. What am I missing?

Upvotes: 1

Views: 36

Answers (1)

Ali Alnadous
Ali Alnadous

Reputation: 334

In your case,

Having table1 with Col 1, Col 2, Col 3...

and table2 with named fields First, Last, Address...

You should follow this:

INSERT INTO table2
(First, Last, Address...) 
SELECT `COL 1` as First, `COl 2` as Last, `COl 3` as Address
FROM table1;

I hope this helped ...

Upvotes: 1

Related Questions