如何在where子句中使用父ID从另一个记录的同一表中获取值

问题描述 投票:0回答:1

在树形结构中,我想在父记录中查找一个值,以查看它在名为nodOpen的字段中是否具有值(对于封闭可以为0,对于开放可以为1)。如果父记录字段nodOpen为1,则当前记录可见,反之亦然。

strSQL = "SELECT nodLevel, nodNum, nodLead, etc., nodParent FROM tblNode WHERE PARENTNODE IS OPEN Visible= 1 ORDER BY nodSort"

用一个SQL语句可能吗?

sql vb.net treeview
1个回答
0
投票

假定表具有以下结构。

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
© www.soinside.com 2019 - 2024. All rights reserved.