当前位置:网站首页>关于#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语句批量执行时间比较快。谢谢
边栏推荐
- Developing Hololens encountered The type or namespace name 'HandMeshVertex' could not be found..
- Mathematics - Properties of Summation Symbols
- 如何在WordPress中添加特定类别的小工具
- Android Practical Development - Kotlin Tutorial (Introduction - Login Function Implementation 3.3)
- Spark基础【介绍、入门WordCount案例】
- Use SuperMap iDesktopX data migration tool to migrate ArcGIS data
- .NET Application -- Helloworld (C#)
- Thinking (88): Use protobuf custom options for multi-version management of data
- 基于生长的棋盘格角点检测方法
- The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined
猜你喜欢

Defect detection (image processing part)

36-Jenkins-Job迁移

运维监控系统之Open-Falcon

Dynamic management of massive service instances

Growth-based checkerboard corner detection method

Swing有几种常用的事件处理方式?如何监听事件?

markdown如何换行——md文件

MySql的索引学习和使用;(本人觉得足够详细)

Use Unity to publish APP to Hololens2 without pit tutorial

阿里本地生活单季营收106亿,大文娱营收72亿,菜鸟营收121亿
随机推荐
AI+PROTAC | dx/tx completes $5 million seed round
数据库设计的酸(ACID)碱(BASE)原则
思考(八十八):使用 protobuf 自定义选项,做数据多版本管理
Is your data safe in this hyperconnected world?
CPDA|How Operators Learn Data Analysis (SQL) from Negative Foundations
Talking about data security governance and privacy computing
[Storage] Dawning Storage DS800-G35 ISCSI maps each LUN to the server
Getting Started with Kubernetes Networking
How to sort multiple fields and multiple values in sql statement
What is the difference between SAP ERP and ORACLE ERP?
36-Jenkins-Job迁移
Beyond YOLO5-Face | YOLO-FaceV2 officially open source Trick+ academic point full
[GYCTF2020]EasyThinking
Redis1:Redis介绍、Redis基本特性、关系型数据库、非关系型数据库、数据库发展阶段
dmp (dump) dump file
冰蝎V4.0攻击来袭,安全狗产品可全面检测
2022-08-04 The sixth group, hidden from spring, study notes
基于生长的棋盘格角点检测方法
On governance and innovation, the 2022 OpenAtom Global Open Source Summit OpenAnolis sub-forum came to a successful conclusion
21 Days Learning Challenge (2) Use of Graphical Device Trees