本文介绍了查询一棵树的数据库表示(MySQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,一个包含节点,第二个包含它们之间的关系,如下所示:

I have two tables, one that contains nodes and second that contains the relationships between them like so:

表A(命名节点)

NodeID | NodeName
 100   |  Name 1
 101   |  Name 2
 102   |  Name 3

表 B(命名关系)

NodeParent | NodeChild
  100      |    101
  101      |    102

我需要执行一个查询,该查询需要找出哪些节点是孤立节点(它们不在关系表中).如何同时搜索 NodeParent 和 NodeChild?

I need to execute a query that needs to find which nodes are orphans (they are not in the Relationship table). How can I search through NodeParent and NodeChild at the same time?

推荐答案

SELECT
  n.NodeID
FROM
  Node AS n
LEFT JOIN
  Relationship AS r
ON
  n.NodeID = r.NodeChild
WHERE
  r.NodeChild IS NULL

这篇关于查询一棵树的数据库表示(MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 02:55