MySQL IFNULL 否则

发布于 2024-09-09 00:50:09 字数 141 浏览 9 评论 0原文

我有一个选择语句,我想在其中设置选择条件,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷︶言冷语的世界 2024-09-16 00:50:09

使用COALESCE

SELECT COALESCE(field_a, field_b)

COALESCE是一个ANSI标准函数返回指定列列表中的第一个非空值,从左到右处理列。因此,在示例中,如果 field_a 为 null,则将显示 field_b 值。但是,如果指定列中没有非空值,则此函数将返回 NULL。

它支持 MySQL(我在 4.1 上使用过)、SQL Server(自 v2000 起)、Oracle 9i+...

Use COALESCE:

SELECT COALESCE(field_a, field_b)

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+...

抱着落日 2024-09-16 00:50:09

以及另一种给猫剥皮的方法(不仅可以灵活地进行空比较)......

select if(field_a is not null, field_a, field_b) from...

and another way to skin that cat (flexible for not just null comparisons)...

select if(field_a is not null, field_a, field_b) from...
瞄了个咪的 2024-09-16 00:50:09

是的,但它只有两个参数:

IFNULL(field_a,field_b)

如果 field_a 不为 null,则返回它。否则将返回field_b

参考: IFNULL

Yes, but it's just two parameters:

IFNULL(field_a,field_b)

If field_a is not null, it will be returned. Otherwise field_b will be returned.

Reference: IFNULL

未央 2024-09-16 00:50:09

该问题已修改,某些答案似乎与当前问题不同步。让我们重新开始吧。
没有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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文