开发者

CASE: WHEN column1 IS NULL THEN write column2

开发者 https://www.devze.com 2023-03-16 11:04 出处:网络
Is it possible to show data from another column if checked column IS NULL? For example: Columns: Color, OriginalColor

Is it possible to show data from another column if checked column IS NULL?

For example:

  • Columns: Color, OriginalColor
  • Table: TableColors [Color, OriginalColor]

    [W, B] [ , G] [B, Y]

And the

SELECT CASE WHEN Color IS NULL "extract the data from Or开发者_开发知识库iginalColor"
FROM TableColors

should get following list: W, G, B


Could you be looking for COALESCE? The function return the first non-NULL value.

SELECT COALESCE(`Color`, `OriginalColor`) AS `Color` FROM `TableColors`;


Check out coalesce - http://www.roseindia.net/sql/mysql-example/mysql-coalesce.shtml


The documentation is quite clear about this:

  • CASE statement http://dev.mysql.com/doc/refman/5.5/en/case-statement.html
  • IF function http://dev.mysql.com/doc/refman/5.5/en/control-flow-functions.html#function_if
  • IFNULL function http://dev.mysql.com/doc/refman/5.5/en/control-flow-functions.html#function_ifnull
  • COALESCE function http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#function_coalesce

The IFNULL() function might be the easiest solution to your problem


SELECT
CASE
WHEN Color IS NULL THEN OriginalColor
ELSE Color
END  AS Color_Or_OriginalColor
FROM TableColors

Edit: one of the many possible ways.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号