【发布时间】:2014-11-02 10:48:00
【问题描述】:
Categories
1 | Pen | 3
2 | Book | 3
3 | Education | null
4 | Shirt | null
Product
1 | 10.00 | Parker-Pen | the description | 1000 | 1
2 | 35.00 | Dairy | the description | 500 | 2
3 | 9.00 | Dux-Pen | the description | 1000 | 1
4 | 350.00 | GeographyMap | the description | 30 | 3
4 | 250.00 | PoloShirt | the description | 100 | 4
这些是我实际检索类别 id 为 3 的产品的表格。
这是我用来检索数据的查询
select p.name, c.name
from product p
inner join Categories c on p.Categories_id=c.id
inner join Categories c2 on c2.id=3 or c2.parent=3
它实际上是在多次检索数据。还有马球衫,它不在类别 ID 上。
你能解释一下有什么问题吗?对产品进行分类的更好方法是什么
【问题讨论】:
-
你能定义数据列的实际含义吗?还有你的预期输出是什么,因为很难理解你想要实现什么。
-
你的预期输出是什么
-
@Ganesh_Devlekar 我想获取类别 id 为 3 的产品以及类别 id 的父 id 为 3 的产品
-
@dotnetom 我想获取类别 id 为 3 的产品以及类别 id 的父 id 为 3 的产品
标签: sql-server database join self-join