MySQL“不在”查询不起作用
我有一个包含三列的表:taxon_id
、scientific_name_element_id
和 parent_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
taxon_name_element.parent_id
中是否有 NULL?查询...
...相当于...
...其中
parent_id_X
是当前位于parent_id
列中的实际值。如果其中之一为 NULL,则对应的taxon_id <> Parent_id_X 表达式将“折叠”为 NULL,并拖动整个 WHERE 表达式。过滤掉 NULL 以获得您想要的:
Are there any NULLs in
taxon_name_element.parent_id
?The query...
...is equivalent to...
...where
parent_id_X
are actual values that are currently in theparent_id
column. If even one of them is NULL, the correspondingtaxon_id <> parent_id_X
expressions will "collapse" into NULL, dragging the whole WHERE expression with it.Filter-out NULLs to get what you want:
假设parent_id列为NULL(表示未设置值)
要选择所有没有parent_id值的scientific_name_element_id > (意味着parent_id 为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:
This will get you a list of scientific_name_element_id that have no parents.