T-SQL 如何从 T-SQL 中的树中获取所有项目? [英] T-SQL How to get all items from a tree in T-SQL?

查看:69
本文介绍了T-SQL 如何从 T-SQL 中的树中获取所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 t-sql 查询有问题.

I have a problem with a t-sql query.

假设我有一个类别树(类别 ID)

Let's say I have a categories tree (categories ID)

cat_table

 1
 |
 2-\
 |  3-\
 6  |  5
 |  4  |
...   ...

ads_table

ad_ID
category_ID

当然 category_ID 列引用了 cat_table

问题是,如何获取(递归?)所有类别的所有广告,其中最顶层的父级是第一个类别?

the problem is, how to get (recursive ?) all advertisements from all categories which the top-most parent is the 1st category?

推荐答案

您是否熟悉 SQL Server 中的公共表表达式?CTE 的众多用途之一是进行递归查询.

Are you familiar with Common Table Expressions in SQL Server? One of the many uses a CTE has is to do recursive queries.

以下是我发现的关于该主题的最佳文章之一:

The following is one of the best articles I've found on the subject:

http://www.4guysfromrolla.com/webtech/071906-1.shtml

这篇关于T-SQL 如何从 T-SQL 中的树中获取所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆