当前位置:网站首页>关于#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语句批量执行时间比较快。谢谢
边栏推荐
猜你喜欢

Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~

Static method to get configuration file data

Question about #sql shell#, how to solve it?

Shell script: for loop and the while loop

The most effective seven performance testing techniques of software testing techniques

ASP.NET application--Hello World

开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..

论治理与创新,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)

Getting Started with Kubernetes Networking
随机推荐
冰蝎V4.0攻击来袭,安全狗产品可全面检测
[Filter tracking] based on matlab unscented Kalman filter inertial navigation + DVL combined navigation [including Matlab source code 2019]
用Unity发布APP到Hololens2无坑教程
From "useable" to "easy to use", domestic software is self-controllable and continues to advance
Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
Linux下常见的开源数据库,你知道几个?
Why did they choose to fall in love with AI?
rpc-remote procedure call demo
Dameng 8 database export and import
AI + Small Nucleic Acid Drugs | Eleven Completes $22 Million Seed Round Financing
Package zip is not available, but is referred to by another package.
How to discover a valuable GameFi?
2022-08-04 第六小组 瞒春 学习笔记
龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
How to find all fields with empty data in sql
How to sort multiple fields and multiple values in sql statement
大佬们,我注意到mysql cdc connector有参数scan.incremental.sna
【 genius_platform software platform development 】 : seventy-six vs the preprocessor definitions written cow force!!!!!!!!!!(in the other groups conding personnel told so cow force configuration to can
十五. 实战——mysql建库建表 字符集 和 排序规则