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.
精彩评论