MySQL“不在”查询不起作用

发布于 2025-01-06 21:45:13 字数 511 浏览 1 评论 0原文

我有一个包含三列的表:taxon_idscientific_name_element_idparent_id。我想找到子元素而不是父元素,因此是结构的终点。

我发现一些资料来源建议我使用

select taxon_id 
       from  taxon_name_element
       where taxon_id not in 
                          (select parent_id from taxon_name_element)

但这不起作用,当我实际浏览条目并看到有一个 taxon_id=1 时,我得到一个空集,并且没有parent_id=1

相反,当我看到parent_id 中的taxon_id 时,我得到一个非空结果集

我做错了什么?我该如何解决这个问题?

I have a table with three columns: taxon_id, scientific_name_element_id, and parent_id. I want to find the elements that are children and not parents, so the termini of the structure.

I found some sources that suggested that I use

select taxon_id 
       from  taxon_name_element
       where taxon_id not in 
                          (select parent_id from taxon_name_element)

But this does not work, I get an empty set when I can actually browse the entries and see that there is, for example, a taxon_id=1, and NO parent_id=1

Conversely when I see what taxon_id's are in parent_id's I get a nonempty result set

What am I doing wrong? How can I fix this?

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

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

发布评论

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

评论(2

网白 2025-01-13 21:45:13

taxon_name_element.parent_id 中是否有 NULL?

查询...

select taxon_id 
from taxon_name_element
where taxon_id not in (
    select parent_id
    from taxon_name_element
)

...相当于...

select taxon_id 
from taxon_name_element
where
    taxon_id <> parent_id_1
    AND taxon_id <> parent_id_2
    ...
    AND taxon_id <> parent_id_N

...其中 parent_id_X 是当前位于 parent_id 列中的实际值。如果其中之一为 NULL,则对应的taxon_id <> Parent_id_X 表达式将“折叠”为 NULL,并拖动整个 WHERE 表达式。

过滤掉 NULL 以获得您想要的:

select taxon_id 
from taxon_name_element
where taxon_id not in (
    select parent_id
    from taxon_name_element
    where parent_id is not null
)

Are there any NULLs in taxon_name_element.parent_id?

The query...

select taxon_id 
from taxon_name_element
where taxon_id not in (
    select parent_id
    from taxon_name_element
)

...is equivalent to...

select taxon_id 
from taxon_name_element
where
    taxon_id <> parent_id_1
    AND taxon_id <> parent_id_2
    ...
    AND taxon_id <> parent_id_N

...where parent_id_X are actual values that are currently in the parent_id column. If even one of them is NULL, the corresponding taxon_id <> parent_id_X expressions will "collapse" into NULL, dragging the whole WHERE expression with it.

Filter-out NULLs to get what you want:

select taxon_id 
from taxon_name_element
where taxon_id not in (
    select parent_id
    from taxon_name_element
    where parent_id is not null
)
逆光下的微笑 2025-01-13 21:45:13

假设parent_id列为NULL(表示未设置值)

要选择所有没有parent_id值的scientific_name_element_id > (意味着parent_id 为NULL)

您这样做:

SELECT scientific_name_element_id
FROM YOUR_TABLE
WHERE parent_id IS NULL

这将为您提供没有父项的science_name_element_id 列表。

Assuming the parent_id column is NULL (meaning no value is set)

To select all scientific_name_element_id that have no value for parent_id (meaning parent_id is NULL)

You do this:

SELECT scientific_name_element_id
FROM YOUR_TABLE
WHERE parent_id IS NULL

This will get you a list of scientific_name_element_id that have no parents.

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