COALESCE()用于空白(但不为空)字段 [英] COALESCE() for blank (but not null) fields
问题描述
I have two fields that I'm comparing with MySQL's function COALESCE(). For example, COALESCE(Field1, Field2)
. The problem is, Field1 is sometimes blank but not null; since it's not null COALESCE()
selects Field1, even though its blank. In that case, I need it to select Field2.
我知道我可以在查询中编写if-then-else(CASE)语句来检查这一点,但是是否有像COALESCE()
这样的简单函数可以用于空白但非空字段?
I know I can write a if-then-else (CASE) statement in the query to check for this, but is there a nice simple function like COALESCE()
for blank-but-not-null fields?
推荐答案
SELECT IFNULL(NULLIF(Field1,''),Field2)
如果Field1为空,则NULLIF返回NULL,如果IFNULL不为空或NULL,则IFNULL返回Field1,否则返回Field2.
NULLIF returns a NULL if Field1 is blank, while IFNULL returns Field1 if it's not blank or NULL and Field2 otherwise.
这篇关于COALESCE()用于空白(但不为空)字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!