当前位置:网站首页>关于#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语句批量执行时间比较快。谢谢
边栏推荐
- YYGH-13-客服中心
- MySql的索引学习和使用;(本人觉得足够详细)
- MRTK3 develops Hololens application - gesture drag, rotate, zoom object implementation
- Ice Scorpion V4.0 attack, security dog products can be fully detected
- rpc-remote procedure call demo
- 达梦8数据库导出导入
- Android Practical Development - Kotlin Tutorial (Introduction - Login Function Implementation 3.3)
- public static
List asList(T... a) What is the prototype? - Dive into how it works together by simulating Vite
- 通过模拟Vite一起深入其工作原理
猜你喜欢

21 Days Learning Challenge (2) Use of Graphical Device Trees

36-Jenkins-Job迁移

public static
List asList(T... a) What is the prototype? 
Solana NFT开发指南

How to Add Category-Specific Widgets in WordPress

Web3.0 Dapps - the road to the future financial world

【七夕节】浪漫七夕,代码传情。将爱意变成绚烂的立体场景,给她(他)一个惊喜!(送代码)

结构体初解

事件解析树Drain3使用方法和解释

The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined
随机推荐
运维监控系统之Open-Falcon
YYGH-13-客服中心
XMjs cross-domain problem solving
Confessing the era of digital transformation, Speed Cloud engraves a new starting point for value
用Unity发布APP到Hololens2无坑教程
The pit of std::string::find return value
905. 区间选点
From "useable" to "easy to use", domestic software is self-controllable and continues to advance
On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
【软件测试】自动化测试之unittest框架
[Qixi Festival] Romantic Tanabata, code teaser.Turn love into a gorgeous three-dimensional scene and surprise her (him)!(send code)
ffmpeg -sources分析
Walter talked little knowledge | "remote passthrough" that something
Initial solution of the structure
达梦8数据库导出导入
[TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.3 Real-time Shadow Introduction
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
龙蜥社区第二届理事大会圆满召开!理事换届选举、4 位特约顾问加入
MySql的索引学习和使用;(本人觉得足够详细)
sql怎么找字段里所有数据为空的字段