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:13 | Current Revision | ||
---|---|---|---|
Content | |||
Unchanged: <p> | Unchanged: <p> | ||
Deleted: I had | Added: I had an issue when I was trying to join two tables with one-to-many relationships. | ||
Unchanged: </p> | Unchanged: </p> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: If there is a table called STUDENTS | Unchanged: If there is a table called STUDENTS | ||
Unchanged: </p> | Unchanged: </p> | ||
Unchanged: <pre> | Unchanged: <pre> | ||
Unchanged: <code>SubjectID StudentName | Unchanged: <code>SubjectID StudentName | ||
Unchanged: ---------- ------------- | Unchanged: ---------- ------------- | ||
Unchanged: 1 Mary | Unchanged: 1 Mary | ||
Unchanged: 1 John | Unchanged: 1 John | ||
Unchanged: 1 Sam | Unchanged: 1 Sam | ||
Unchanged: 2 Alaina | Unchanged: 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 StudentName | Unchanged: <code>SubjectID StudentName | ||
Unchanged: ---------- ------------- | Unchanged: ---------- ------------- | ||
Unchanged: 1 Mary, John, Sam | Unchanged: 1 Mary, John, Sam | ||
Unchanged: 2 Alaina, Edward</code></pre> | Unchanged: 2 Alaina, Edward</code></pre> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: | Unchanged: | ||
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: | Unchanged: | ||
Unchanged: </p> | Unchanged: </p> | ||
Unchanged: <p> | Unchanged: <p> | ||
Unchanged: In MySQL there is a function, <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, <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 ', ') AS people | Unchanged: <code>SELECT 1 AS a, GROUP_CONCAT(name ORDER BY name ASC SEPARATOR ', ') 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: | Unchanged: | ||
Unchanged: </p> | Unchanged: </p> |
Note: Spaces may be added to comparison text to allow better line wrapping.
No comments yet.