user1987363
user1987363

Reputation: 31

insert data from multiple rows to one row mysql

Ok here's the thing. I need to insert multiple rows in one column from table A to one row in table B using MySql

Here's an example of what im trying to achieve:

Table A
id | data
1  | name
2  | date
3  | more

From table A, i need to select all the rows in the data column and insert that data in one row of 'data' column of table B like this:

Table B 
id | data
1  | name, date, more

Here is my current code, however it isn't working and shows me an error subquery returns more than one row

INSERT INTO B (data) values (SELECT data from A)

Is there any way i can do this? Please excuse as i am a newbie :P

Thanks in advance!

Upvotes: 3

Views: 1567

Answers (1)

Saharsh Shah
Saharsh Shah

Reputation: 29051

Use GROUP_CONCAT function

Try this:

INSERT INTO B (data) 
SELECT GROUP_CONCAT(data) FROM A;

Upvotes: 1

Related Questions