mysql CLAUSE 中的 NULL 值

发布于 2024-09-26 22:28:01 字数 319 浏览 4 评论 0原文

如何处理 mysql where in 子句中的 NULL

我尝试像

SELECT * FROM mytable WHERE field IN(1,2,3,NULL)

它不起作用

一样:

SELECT * FROM mytable WHERE field IN(1,2,3) OR field IS NULL

我怎样才能让它在 WHERE IN ?有可能吗?

how to deal with NULL value in mysql where in CLAUSE

i try like

SELECT * FROM mytable WHERE field IN(1,2,3,NULL)

it not working

only work like :

SELECT * FROM mytable WHERE field IN(1,2,3) OR field IS NULL

how can i get it work in WHERE IN ? it is possible ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

南汐寒笙箫 2024-10-03 22:28:01

有一个名为 COALESCE。它返回列表中的第一个非NULL 值,如果没有非NULL 值,则返回NULL

例如,如果您运行SELECT COALESCE(NULL, NULL, -1);,您将得到-1,因为它是第一个非NULL价值。

因此,这里的技巧是将表达式包装在 COALESCE 中,并添加一个值作为最后一个参数,该参数也在 IN 函数中添加。

SELECT * FROM mytable WHERE COALESCE(field,-1) IN (1,2,3,-1)

仅当字段为 1,2 或 3,或者字段为 NULL 时才会匹配。

There is a MySQL function called COALESCE. It returns the first non-NULL value in the list, or NULL if there are no non-NULL values.

If you for example run SELECT COALESCE(NULL, NULL, -1); you will get -1 back because it's the first non-NULL value.

So the trick here is to wrap your expression in COALESCE, and add a value as the last parameter that you also add in your IN function.

SELECT * FROM mytable WHERE COALESCE(field,-1) IN (1,2,3,-1)

It will only match if field is 1,2 or 3, or if field is NULL.

心房的律动 2024-10-03 22:28:01

也许MySQL参考手册中的信息有帮助:

为了符合 SQL 标准,IN 不仅在左侧表达式为 NULL 时返回 NULL,而且在列表中未找到匹配项并且列表中的表达式之一为 NULL< /strong>.


Maybe this information from the MySQL Reference Manual helps:

To comply with the SQL standard, IN returns NULL not only if the expression on the left hand side is NULL, but also if no match is found in the list and one of the expressions in the list is NULL.

故事还在继续 2024-10-03 22:28:01

据我了解,您希望提取具有 1、2、3 和 null 值的每条记录。

我认为不可能将 null 放入 IN 运算符中。它需要值,而 null 很好..不是一个值。因此,您确实必须将“或”与“空”进行“或”运算才能获得所需的结果。

As by my understanding you want to pull every record with 1,2,3 and null value.

I don't think its possible to put null in the IN operator. Its expects values and null is well.. not a value. So You really have to put the OR with the null to get the desired result.

洛阳烟雨空心柳 2024-10-03 22:28:01

在 IN 运算符中使用 UNION 作为子查询可以获取 tableIds 作为列表,并从中获取具有 NULL 值的结果。

例如:

SELECT * FROM
mytable 
WHERE mytable.id IN(
    SELECT mytable.id
    FROM mytable
    where mytable.field IS NULL
    UNION
    SELECT mytable.id 
    FROM mytable
    WHERE mytable.field IN(1,2,3)
    )

Using UNION as a subquery in IN operator can get tableIds as a list and from that can get results with the NULL value.

eg:

SELECT * FROM
mytable 
WHERE mytable.id IN(
    SELECT mytable.id
    FROM mytable
    where mytable.field IS NULL
    UNION
    SELECT mytable.id 
    FROM mytable
    WHERE mytable.field IN(1,2,3)
    )
套路撩心 2024-10-03 22:28:01

以下声明应该有所帮助:

SELECT * FROM mytable WHERE COALESCE(field,0) IN (1,2,3,0)

Following statement should help:

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