You are viewing an old revision of this post, from June 30, 2016 @ 15:46:13. See below for differences between this version and the current revision.

MySql – Concatenate many rows into a single text string

I had an issue when I was trying to join two tables with one-to-many relationships. 

If there is a table called STUDENTS

SubjectID       StudentName
----------      -------------
1               Mary
1               John
1               Sam
2               Alaina
2               Edward

Result I expected was:

SubjectID       StudentName
----------      -------------
1               Mary, John, Sam
2               Alaina, Edward

 

* Solution:

 

In MySQL there is a function, GROUP_CONCAT(), which allows you to concatenate the values from multiple rows. Example:

SELECT 1 AS a, GROUP_CONCAT(name ORDER BY name ASC SEPARATOR ', ') AS people 
FROM users 
WHERE id IN (1,2,3) 
GROUP BY a

 

Revisions

  • June 30, 2016 @ 15:47:10 [Current Revision] by admin
  • June 30, 2016 @ 15:47:10 by admin
  • June 30, 2016 @ 15:46:13 by admin

Revision Differences

June 30, 2016 @ 15:46:13Current Revision
Content
Unchanged: <p>Unchanged: <p>
Deleted: I had a similar issue when I was trying to join two tables with one-to-many relationships. In SQL 2005 I found that XML PATH method can handle the concatenation of the rows very easily. Added: I had an&nbsp;issue when I was trying to join two tables with one-to-many relationships.&nbsp;
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: If there is a table called STUDENTSUnchanged: If there is a table called STUDENTS
Unchanged: </p>Unchanged: </p>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>SubjectID StudentNameUnchanged: <code>SubjectID StudentName
Unchanged: ---------- -------------Unchanged: ---------- -------------
Unchanged: 1 MaryUnchanged: 1 Mary
Unchanged: 1 JohnUnchanged: 1 John
Unchanged: 1 SamUnchanged: 1 Sam
Unchanged: 2 AlainaUnchanged: 2 Alaina
Unchanged: 2 Edward</code></pre>Unchanged: 2 Edward</code></pre>
Unchanged: <p>Unchanged: <p>
Unchanged: Result I expected was:Unchanged: Result I expected was:
Unchanged: </p>Unchanged: </p>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>SubjectID StudentNameUnchanged: <code>SubjectID StudentName
Unchanged: ---------- -------------Unchanged: ---------- -------------
Unchanged: 1 Mary, John, SamUnchanged: 1 Mary, John, Sam
Unchanged: 2 Alaina, Edward</code></pre>Unchanged: 2 Alaina, Edward</code></pre>
Unchanged: <p>Unchanged: <p>
Unchanged: &nbsp;Unchanged: &nbsp;
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: <strong>* Solution:</strong>Unchanged: <strong>* Solution:</strong>
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: &nbsp;Unchanged: &nbsp;
Unchanged: </p>Unchanged: </p>
Unchanged: <p>Unchanged: <p>
Unchanged: In MySQL there is a function,&nbsp;<a href="http:// dev.mysql.com/doc/refman/ 5.0/en/group- by-functions.html#function_ group-concat" >GROUP_CONCAT()</a>, which allows you to concatenate the values from multiple rows. Example:Unchanged: In MySQL there is a function,&nbsp;<a href="http:// dev.mysql.com/doc/refman/ 5.0/en/group- by-functions.html#function_ group-concat" >GROUP_CONCAT()</a>, which allows you to concatenate the values from multiple rows. Example:
Unchanged: </p>Unchanged: </p>
Unchanged: <pre>Unchanged: <pre>
Unchanged: <code>SELECT 1 AS a, GROUP_CONCAT(name ORDER BY name ASC SEPARATOR &#39;, &#39;) AS people Unchanged: <code>SELECT 1 AS a, GROUP_CONCAT(name ORDER BY name ASC SEPARATOR &#39;, &#39;) AS people
Unchanged: FROM users Unchanged: FROM users
Unchanged: WHERE id IN (1,2,3) Unchanged: WHERE id IN (1,2,3)
Unchanged: GROUP BY a</code></pre>Unchanged: GROUP BY a</code></pre>
Unchanged: <p>Unchanged: <p>
Unchanged: &nbsp;Unchanged: &nbsp;
Unchanged: </p>Unchanged: </p>

Note: Spaces may be added to comparison text to allow better line wrapping.

No comments yet.

Leave a Reply