Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <a href="http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat" rel="noreferrer"><code>GROUP_CONCAT</code></a>:</p> <pre><code>SELECT person_id, GROUP_CONCAT(hobbies SEPARATOR ', ') FROM peoples_hobbies GROUP BY person_id </code></pre> <p>As Ludwig stated in <a href="https://stackoverflow.com/questions/276927/can-i-concatenate-multiple-mysql-rows-into-one-field#comment14513101_276949">his comment,</a> you can add the <code>DISTINCT</code> operator to avoid duplicates:</p> <pre><code>SELECT person_id, GROUP_CONCAT(DISTINCT hobbies SEPARATOR ', ') FROM peoples_hobbies GROUP BY person_id </code></pre> <p>As Jan stated in <a href="https://stackoverflow.com/questions/276927/can-i-concatenate-multiple-mysql-rows-into-one-field#comment72475644_276949">their comment,</a> you can also sort the values before imploding it using <code>ORDER BY</code>:</p> <pre><code>SELECT person_id, GROUP_CONCAT(hobbies ORDER BY hobbies ASC SEPARATOR ', ') FROM peoples_hobbies GROUP BY person_id </code></pre> <p>As Dag stated in <a href="https://stackoverflow.com/questions/276927/can-i-concatenate-multiple-mysql-rows-into-one-field/276949#comment12638055_276949">his comment,</a> there is a 1024 byte limit on the result. To solve this, run this query before your query:</p> <pre><code>SET group_concat_max_len = 2048 </code></pre> <p>Of course, you can change <code>2048</code> according to your needs. To calculate and assign the value:</p> <pre><code>SET group_concat_max_len = CAST( (SELECT SUM(LENGTH(hobbies)) + COUNT(*) * LENGTH(', ') FROM peoples_hobbies GROUP BY person_id) AS UNSIGNED ) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload