返回视图中的两列之一 - 无论哪一列不为空 [英] Return one of two columns in a view - whichever one is not null
问题描述
我有一个包含三列的表格:
I have a table with three columns:
ColumnA ColumnB ColumnC
AAA NULL 123
BBB 222 NULL
CCC NULL NULL
我想创建一个 SELECT 语句,它将返回 ColumnA,然后是第二列,除非 ColumnB 为空,否则它将显示 ColumnB 的值;否则它会显示 ColumnC 的值,即使它是 NULL.我可以为此使用 IF 语句吗?类似的东西:
I would like to create a SELECT statement which will return ColumnA, and then a second column which will either show the value of ColumnB unless ColumnB is null; otherwise it will show the value of ColumnC, even it it's NULL. Can I use an IF statement for that? Something like:
SELECT ColumnA,
IF(ColumnB IS NULL, ColumnC, ColumnB)
FROM table
**如果我让它工作,下一步将返回连接列的值而不是 ColumnB.实际上,IF 语句将是
**If I get this working, the next step would be to return the value of a joined column instead of ColumnB. In effect the IF statement would be
IF(table.ColumnB IS NULL, table.ColumnC, table2.ColumnD)
推荐答案
阅读到问题的结尾,听起来您需要使用 CASE
Reading to the end of your question it sounds like you need to use CASE
CASE WHEN table.ColumnB IS NULL
THEN table.ColumnC
ELSE table2.ColumnD
END AS Whatever
这篇关于返回视图中的两列之一 - 无论哪一列不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!