Thursday 25 June 2020

How to group mysql rows with same column value into one row?

https://stackoverflow.com/questions/3664393/how-to-group-mysql-rows-with-same-column-value-into-one-row

How to group mysql rows with same column value into one row?

Use GROUP_CONCAT() like this:
 SELECT k.id, GROUP_CONCAT(d.value)
  FROM keywords AS k
  INNER JOIN data as d ON k.id = d.id
  GROUP BY k.id
Also, you may need to do ORDER BY d.name to get exact order of values as you want. Like this:
 SELECT k.id, GROUP_CONCAT(d.value ORDER BY d.name separator ' ')
  FROM keywords AS k
  INNER JOIN data as d ON k.id = d.id
  GROUP BY k.id

No comments:

Post a Comment

Note: only a member of this blog may post a comment.