skip to Main Content

I have a database with email and phone columns. One email can have several phones, which results in multiple rows with same email but different phone numbers.

I want to query all emails with it’s phones grouped in one single column.

Example

Convert from this

11111  [email protected]
22222  [email protected]  
33333  [email protected]
44444  [email protected]

To this

[email protected]  11111, 22222, 33333, 44444

Thanks!

2

Answers


  1. Chosen as BEST ANSWER

    I found it

    select email,GROUP_CONCAT(tel1,',',tel2) as phones from table group by email;
    

    Thanks!


  2. group_concat to the rescue:

    SELECT   email, GROUP_CONCAT(other ORDER BY other ASC SEPARATOR ', ') 
    FROM     mytable
    GROUP BY email
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search