Home > Software engineering >  How to combine datas with same column and ignore null values in mysql
How to combine datas with same column and ignore null values in mysql

Time:12-07

I'm just going to ask how to query combine datas with same column and ignore all null values

actual table:

 id |   first_name  |   last name  |    age   |   unique_id 
  1 |      Doe      |              |          |         111
  2 |               |     John     |          |         111
  3 |               |              |     32   |         111
  4 |      Reeves   |              |          |         222
  5 |               |      Keanu   |          |         222

wanted query result :

      first_name  |   last name  |    age   |   unique_id 
          Doe     |     John     |     32   |      111
          Keanu   |     Reeves   |          |      222
  

Thanks!!

CodePudding user response:

Aggregate by unique_id and use MAX:

SELECT
    unique_id,
    MAX(first_name) AS first_name,
    MAX(last_name) AS last_name,
    MAX(age) AS age
FROM yourTable
GROUP BY
    unique_id;
  • Related