如果不为null,则Sql选择字段值 [英] Sql select field value if not null
本文介绍了如果不为null,则Sql选择字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含5个字段的SQL数据库,我想要一个select语句,如果它不是null,将从一个字段中选择值,否则它将从第二个字段中选择。我找到了案件情况,但不知道如何陈述。
SELECT CASE
WHEN tel 不 null
那么 选择 tel
ELSE 选择 mob
END as 可售
FROM 产品
我的尝试:
到目前为止我没有尝试过,但我会尝试上面的代码
解决方案
最简单的方法是使用 COALESCE功能 [ ^ ]。
SELECT COALESCE (tel,mob) as 可售
FROM 产品
使用CASE ... WHEN .. .END
声明:
SELECT CASE WHEN tel IS NULL 那么 mob ELSE tel END as 已售出
FROM 产品
试试这个
SELECT CASE
WHEN tel 不 null
那么 tel
ELSE mob
END as 已售出
FROM 产品
I have an SQL database with 5 fields, I want a select statement that will choose value from one field if it is not null or else it will choose from second. I found the case condition but not sure how to state it.
SELECT CASE
WHEN tel is not null
THEN select tel
ELSE select mob
END as Saleable
FROM Product
What I have tried:
I have tried nothing so far but I would try the above code
解决方案
The simplest way is to use COALESCE function[^].
SELECT COALESCE(tel, mob) as Saleable FROM Product
UsingCASE ... WHEN ... END
statement:
SELECT CASE WHEN tel IS NULL THEN mob ELSE tel END as Saleable FROM Product
try this
SELECT CASE WHEN tel is not null THEN tel ELSE mob END as Saleable FROM Product
这篇关于如果不为null,则Sql选择字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文