开发者

Select row which has all five under it in the "tree"

开发者 https://www.devze.com 2022-12-18 12:27 出处:网络
I have two tables. One is article and the other structure. And the articles can be viewed as a tree with childnodes and so on. Li开发者_运维百科ke the Windows forms control TreeView.

I have two tables. One is article and the other structure. And the articles can be viewed as a tree with childnodes and so on. Li开发者_运维百科ke the Windows forms control TreeView.

The structure table basically looks this:

  • article_id
  • article_above_id

Article:

  • article_id
  • article_number

I want to select from a maximum of five article_number's and from that show the article which has these articles under it in the tree.

The article_number-data comes from a GUI where at least one is required and will be null if nothing entered.


You can select the five articles and join in their parent articles, group on the parent articles and get only the articles that has five matches for child articles.

select p.article_id
from Article a
inner join Structure s on s.article_above_id = article_id
inner join Article p on p.article_id = s.article_id
where a.article_number in (3,7,45,186,203)
group by p.article_id
having count(*) = 5

(Note: I used the "above" field in the structure table to mean above in the tree, i.e. the id of the child item. If you have turned the tree upside down and have the leaves hanging under the root, you will have to switch the use of the fields.)

0

精彩评论

暂无评论...
验证码 换一张
取 消