如何使用 where 子句中的父 ID 从另一条记录中的相同 table 获取值
How to get a value from the same table in another record using a parent id in a where clause
在树结构中,我想在父记录中查找一个值,看看它在名为 nodOpen 的字段中是否有值(可以是 0 表示关闭,1 表示打开)。如果父记录字段 nodOpen 为 1,则当前记录可见,反之亦然。
strSQL = "SELECT nodLevel, nodNum, nodLead, etc., nodParent FROM tblNode WHERE PARENTNODE IS OPEN Visible= 1 ORDER BY nodSort"
用 SQL 一句话就可以做到吗?
我们假设 table 具有以下结构。
create table tblNode (
nodNum int not null,
nodLevel int,
nodOpen int not null,
nodSort int not null,
nodLead int not null,
nodParent int null,
-- insert other columns here(?)
constraint PK_tblNode primary key clustered (nodNum),
constraint FK_tblNode_tree foreign key (nodParent)
references tblNode(nodNum)
);
查询看起来像这样。
select
nodLevel,
nodNum,
nodLead,
nodParent,
nodSort -- you may have to include this column to sort on it later
/* insert other columns here(?) */
from tblNode as childNode
inner join tblNode as parentNode on parentNode.nodNum = childNode.nodParent
where parentNode.nodOpen = 1
order by nodSort
在树结构中,我想在父记录中查找一个值,看看它在名为 nodOpen 的字段中是否有值(可以是 0 表示关闭,1 表示打开)。如果父记录字段 nodOpen 为 1,则当前记录可见,反之亦然。
strSQL = "SELECT nodLevel, nodNum, nodLead, etc., nodParent FROM tblNode WHERE PARENTNODE IS OPEN Visible= 1 ORDER BY nodSort"
用 SQL 一句话就可以做到吗?
我们假设 table 具有以下结构。
create table tblNode (
nodNum int not null,
nodLevel int,
nodOpen int not null,
nodSort int not null,
nodLead int not null,
nodParent int null,
-- insert other columns here(?)
constraint PK_tblNode primary key clustered (nodNum),
constraint FK_tblNode_tree foreign key (nodParent)
references tblNode(nodNum)
);
查询看起来像这样。
select
nodLevel,
nodNum,
nodLead,
nodParent,
nodSort -- you may have to include this column to sort on it later
/* insert other columns here(?) */
from tblNode as childNode
inner join tblNode as parentNode on parentNode.nodNum = childNode.nodParent
where parentNode.nodOpen = 1
order by nodSort