• 5th September 2006 -By Vinu Thomas

    GROUP_CONCAT(expr) – This function returns a string result with the concatenated non-NULL values from a group.

    Where it can be useful?

    For example to get PHP array without looping inside PHP:
    CREATE TABLE services (
    id INT UNSIGNED NOT NULL,
    client_id INT UNSIGNED NOT NULL,
    KEY (id));
    INSERT INTO services
    VALUES (1,1),(1,2),(3,5),(3,6),(3,7);
    SELECT id,client_id FROM services WHERE id = 3;
    +----+-----------+
    | id | client_id
    +----+-----------+
    | 3 | 5
    | 3 | 6
    | 3 | 7
    +----+-----------+
    SELECT id,GROUP_CONCAT(client_id) FROM services WHERE id = 3 GROUP BY id;
    +----+-------------------------+
    | id | GROUP_CONCAT(client_id)
    +----+-------------------------+
    | 3 | 5,6,7
    +----+-------------------------+

    Read more: MySQL Performance Blog » GROUP_CONCAT useful GROUP BY extension

  • 7 Comments to “GROUP_CONCAT in MySQL”

    • Hrashad M on November 13, 2008

      This is really a very useful function in Business Logic !!
      Thank You…

    • iftee on February 14, 2009

      what if I want to separate them with something different than just ” , ” (comma).

      thanks in advance..

    • vinu on February 15, 2009

      You can define your separator -
      GROUP_CONCAT(client_id SEPARATOR ‘:’)
      Check the GROUP_CONCAT definition over at:
      http://gourl.in/2d

    • vipin on April 18, 2009

      This is very interesting function

    • vipin on April 18, 2009

      This is very interesting function

    • Nishant on April 19, 2010

      SELECT GROUP_CONCAT(userId SEPARATOR ':') FROM …..

    • Sanjay Soral on July 25, 2010

      Outstanding function, Thank you Very Much. Great………

    Leave a Reply