Search code examples
mysqljoinselectnullrow

MySQL - group rows with different columns discarding Null values


I am fairly new and need help with this!

How can I do a SELECT in MySQL to obtain from the following Table a result that joins all the rows in one with its columns?

Table:

ID Name Type1 Type2 Type3 Type4
1 ABC 123 Null Null Null
1 ABC Null 456 Null Null
1 ABC Null Null 789 Null
1 ABC Null Null Null 900

Output:

ID Name Type1 Type2 Type3 Type4
1 ABC 123 456 789 900

I was researching and I don't even know how to search for the function! Thank you very much


Solution

  • exact question was asked here

    SELECT ID,
           NAME,
           MAX(Type1) AS Type1,
           MAX(Type2) AS Type2,
           MAX(Type3) AS Type3
    FROM tableName
    GROUP BY ID,
             NAME;