当前位置:网站首页>关于#SQL#的迭代、父子结构查询问题,如何解决?
关于#SQL#的迭代、父子结构查询问题,如何解决?
2022-08-05 03:32:00 【CSDN问答】
我用的SQLSERVER数据库,有一个商品分类表,一个商品表。 商品分类表中有上下级关系,用parentid来连接。商品表中一个商品只有一个分类ID,现在想查询所有商品的分类及分类上上级、上上级 。想加个中间表来保存商品和分类的关系,以便于查询。C#用的EF。
数据表参考
商品表
| ID | CategoryID | 商品名称 |
|---|---|---|
| 1 | 100101 | 商品1 |
| 2 | 2001 | 商品2 |
分类表
| ID | ParentID | 分类名称 |
|---|---|---|
| 10 | NULL | 分类1 |
| 1001 | 10 | 分类11 |
| 100101 | 1001 | 分类111 |
| 20 | NULL | 分类2 |
| 2001 | 20 | 分类22 |
想要查询的结果
| 商品ID | 分类ID |
|---|---|
| 1 | 10 |
| 1 | 1001 |
| 1 | 100101 |
| 2 | 20 |
| 2 | 2001 |
期望能给我sql语句,不用程序来实现。数据量比较大,sql语句批量执行时间比较快。谢谢
边栏推荐
- 为什么pca分量没有关联
- 惨遭打脸:字节某部门竟有这么多测试员
- [论文笔记] MapReduce: Simplified Data Processing on Large Clusters
- Hard power or soft power, which is more important to testers?
- ffmpeg pixel format basics
- public static
List asList(T... a) What is the prototype? - Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
- 告白数字化转型时代,时速云镌刻价值新起点
- 905. 区间选点
- 剑指Offer--找出数组中重复的数字(三种解法)
猜你喜欢

通过模拟Vite一起深入其工作原理

How to discover a valuable GameFi?

Flink 1.15.1 Cluster Construction (StandaloneSession)

论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕

沃谈小知识 |“远程透传”那点事儿
![[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)](/img/10/dafea90158adf9d43c4f025414fef7.png)
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)

leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
![[GYCTF2020]EasyThinking](/img/40/973411c69d1e4766d22f6a4a7c7c01.png)
[GYCTF2020]EasyThinking

告白数字化转型时代,时速云镌刻价值新起点

Step by step how to perform data risk assessment
随机推荐
论治理与创新,2022 开放原子全球开源峰会 OpenAnolis 分论坛圆满落幕
高项 02 信息系统项目管理基础
The sword refers to Offer--find the repeated numbers in the array (three solutions)
Distributed systems revisited: there will never be a perfect consistency scheme...
Dameng 8 database export and import
Solana NFT开发指南
2022-08-04 The sixth group, hidden from spring, study notes
.NET Application -- Helloworld (C#)
银行数据采集,数据补录与指标管理3大问题如何解决?
dmp (dump) dump file
Fifteen. Actual combat - MySQL database building table character set and collation
静态方法获取配置文件数据
数组常用方法总结
Getting Started with Kubernetes Networking
presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
[TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.3 Real-time Shadow Introduction
Detailed and comprehensive postman interface testing practical tutorial
毕设-基于SSM房屋租赁管理系统
AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
思考(八十八):使用 protobuf 自定义选项,做数据多版本管理