Select column value if not null else use another column value

后端 未结 3 622
心在旅途
心在旅途 2021-02-13 04:01

I have 2 columns in mysql table: a and b. a is allways string value and b is sometimes a string value and sometimes it is null.

How to construct a mysql SELECT so that t

相关标签:
3条回答
  • 2021-02-13 04:23

    Use IFNULL(b, a).

    If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2.

    This is a MySQL specific function. You can also use COALESCE in the same way. This will work in more databases but it has slightly worse performance in MySQL than IFNULL.

    0 讨论(0)
  • 2021-02-13 04:25

    IFNULL(b, a) ?

    0 讨论(0)
  • 2021-02-13 04:35
    IF(expr1,expr2,expr3)
    

    If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3.

    0 讨论(0)
提交回复
热议问题