当前位置:网站首页>关于#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语句批量执行时间比较快。谢谢
边栏推荐
- STM32 uses stm32cubemx LL library series tutorial
- Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
- Call Alibaba Cloud oss and sms services
- 思考(八十八):使用 protobuf 自定义选项,做数据多版本管理
- 包拉链不可用,但是是被另一个包。
- Intersection of Boolean Operations in SuperMap iDesktop.Net - Repairing Complex Models with Topological Errors
- 大像素全景制作完成后,推广方式有哪些?
- 36-Jenkins-Job迁移
- .NET应用程序--Helloworld(C#)
- Common open source databases under Linux, how many do you know?
猜你喜欢

七夕节代码表白

public static <T> List<T> asList(T... a) 原型是怎么回事?

.NET Application -- Helloworld (C#)

leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)

Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals

Why did they choose to fall in love with AI?

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

Use CH341A to program external Flash (W25Q16JV)

Kubernetes 网络入门

The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined
随机推荐
Step by step how to perform data risk assessment
Details such as compiling pretreatment
【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)
Spark基础【介绍、入门WordCount案例】
Use CH341A to program external Flash (W25Q16JV)
How to simulate the background API call scene, very detailed!
Fifteen. Actual combat - MySQL database building table character set and collation
Detailed and comprehensive postman interface testing practical tutorial
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
2022高处安装、维护、拆除考试题模拟考试题库及在线模拟考试
Use Unity to publish APP to Hololens2 without pit tutorial
银行数据采集,数据补录与指标管理3大问题如何解决?
毕设-基于SSM房屋租赁管理系统
剑指Offer--找出数组中重复的数字(三种解法)
dmp (dump) dump file
ffmpeg 枚举decoders, encoders 分析
Dive into how it works together by simulating Vite
Why did they choose to fall in love with AI?