MySQL IFNULL 否则
我有一个选择语句,我想在其中设置选择条件,如下所示:
IFNULL(field_a, field_a, field_b)
以便它检查字段 a。如果 a 为空,则选择将是字段 b。
这可能吗?
I have a select statement where I want to make the select conditional like this:
IFNULL(field_a, field_a, field_b)
so that it checks field a. If a is null then the select would be field b.
Is that possible ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用COALESCE:
COALESCE是一个ANSI标准函数返回指定列列表中的第一个非空值,从左到右处理列。因此,在示例中,如果
field_a
为 null,则将显示field_b
值。但是,如果指定列中没有非空值,则此函数将返回 NULL。它支持 MySQL(我在 4.1 上使用过)、SQL Server(自 v2000 起)、Oracle 9i+...
Use COALESCE:
COALESCE is an ANSI standard function that returns the first non-null value from the list of columns specified, processing the columns from left to right. So in the example, if
field_a
is null,field_b
value will be displayed. However, this function will return NULL if there is no non-null value from the columns specified.It's supported on MySQL (I've used it on 4.1), SQL Server (since v2000), Oracle 9i+...
以及另一种给猫剥皮的方法(不仅可以灵活地进行空比较)......
and another way to skin that cat (flexible for not just null comparisons)...
是的,但它只有两个参数:
如果
field_a
不为 null,则返回它。否则将返回field_b
。参考: IFNULL
Yes, but it's just two parameters:
If
field_a
is not null, it will be returned. Otherwisefield_b
will be returned.Reference: IFNULL
该问题已修改,某些答案似乎与当前问题不同步。让我们重新开始吧。
没有IFNULL(test, NullCase, notNullCase),但是有switch语句和else。在这个例子中,我做了一个左连接来查找是否有一个块,所以,但我有同样的问题,因为我想要一个 0 或 1。这是我的解决方案。
选择
案例
当 person.blocked 为 NULL 时,则 0
其他 1
END AS 被阻止
来自我的表
The question was modified, and it appears some answers are not in sync with the current question. Lets start over.
there is no IFNULL(test, NullCase, notNullCase), but there is a switch statement with else. In this example, i did a left join to find if there is a block, so, but I had the same question, because I want a 0 or 1. here is my solution.
select
CASE
WHEN person.blocked is NULL THEN 0
ELSE 1
END AS isBlocked
from mytable