Ank
Ank

Reputation: 6270

Copy from one column to another (different tables same database) mysql

Hi I would like to copy entire contents from column Item under table IName to column Name under table Item both belonging to the same database.

I am giving the following query but it throws the error saying that the subquery returned more than one records. (There are around 600 records)

Insert into Item set name = (Select Item from IName)

Thanks

Upvotes: 3

Views: 19982

Answers (3)

konsolenfreddy
konsolenfreddy

Reputation: 9671

INSERT INTO table_one (column1) SELECT column2 FROM table_two

See MySQL Ref

Upvotes: 4

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115530

INSERT INTO Item (Name)
  SELECT Item 
  FROM IName

When you want to insert into a single-column* table, INSERT works either with:

INSERT INTO table (column)
  VALUES (value1),(value2), ... (valueN) ;

or with:

INSERT INTO table (column)
  SELECT a_column 
  FROM a_table
                       --- optional (multiple) JOINs
                       --- and WHERE
                       --- and GROUP BY      
                       --- any complex SELECT query

(OK, the above can work with a multiple-column table, too, as long as all the other - not explicitely stated in the INSERT statement - columns have been defined with a DEFAULT value or with AUTO_INCREMENT.)


The INSERT ... SET syntax is valid in MySQL only and can be used only when you want to insert one row exactly:

INSERT INTO table 
  SET column = value1 ;

is equivalent to:

INSERT INTO table (column)
  VALUES (value1) ;

Upvotes: 10

xbrady
xbrady

Reputation: 1703

INSERT INTO Item (name)
SELECT Item FROM IName

Link

Upvotes: 4

Related Questions