Logo Background RSS

GROUP_CONCAT in MySQL

  • Written by vinuvinu No Comments Comments
    Last Updated: September 5th, 2006

    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

    Bookmark and share:
    • del.icio.us
    • Digg
    • StumbleUpon
    • BlinkList
    • blogmarks
    • Furl
    • Slashdot
    • Spurl
    • Technorati
    • YahooMyWeb
    • description
    • Facebook
    • Google
    • Live
    • Ma.gnolia
    • NewsVine
    • Reddit
    • TwitThis

Advertisement

Leave a Comment