Replace Default Null Values Returned From Left Outer Join

后端 未结 3 803
夕颜
夕颜 2020-12-07 19:40

I have a Microsoft SQL Server 2008 query that returns data from three tables using a left outer join. Many times, there is no data in the second and third tables and so I g

3条回答
  •  时光说笑
    2020-12-07 20:05

    MySQL

    COALESCE(field, 'default')
    

    For example:

      SELECT
        t.id,
        COALESCE(d.field, 'default')
      FROM
         test t
      LEFT JOIN
         detail d ON t.id = d.item
    

    Also, you can use multiple columns to check their NULL by COALESCE function. For example:

    mysql> SELECT COALESCE(NULL, 1, NULL);
            -> 1
    mysql> SELECT COALESCE(0, 1, NULL);
            -> 0
    mysql> SELECT COALESCE(NULL, NULL, NULL);
            -> NULL
    

提交回复
热议问题