0

I have searched quite a lot but haven´t found a solution to my problem. Now I hope someone can help me here.

I have two tables in an MySQL database and both tables have the columns Name and ArticleID

What I would like to do is to copy the content in the column Name from Table 1 to Table 2 where the ArticleID match. All the names should be separated by a comma in the column Name in Table 2.

In Table 1 there are for example 3 rows with the same content in the column Name but each row has a unique ArticleID. In 3 other rows there are a different Name but the ArticleID´s is the same as the first 3 rows.

Table 1

Name 1 - 1
Name 2 - 2
Name 3 - 3
Name 4 - 1
Name 5 - 2
Name 6 - 3

Table 2

1 - Name 1, Name 4
2 - Name 2, Name 5
3 - Name 3, Name 6

This would not normally be a problem for me, But now there are multiple rows with the same ArticleID and i can´t seem to figure it out by my self.

I hope you understand what I want :-)

Melker

4

2 回答 2

1
INSERT INTO TABLE2(id, names)
SELECT ArticleID, GROUP_CONCAT(Name) 
FROM TABLE1 GROUP BY ArticleID;
于 2013-09-25T12:29:40.807 回答
0
UPDATE 
    TABLE2 
JOIN (SELECT ArticleID, GROUP_CONCAT(Name) AS Name FROM TABLE1 GROUP BY ArticleID) TABLE1
ON TABLE2.ArticleID = TABLE1.ArticleID
SET TABLE2.Name = TABLE1.Name
WHERE TABLE2.ArticleID = TABLE1.ArticleID

我实际上不得不使用 UPDATE 并且这有效

于 2013-09-25T20:56:38.620 回答