sql查询自引用表查询 [英] sql query self reference table query

查看:199
本文介绍了sql查询自引用表查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为类别字段categoryid,category,parentcategoryid的表.
这是自我参照表.
如果parentcategoryid = 0,则表示其父类别;如果父类别不为0,则表示其是子类别..
我想要选择查询中的2个字段,其中一个仅返回父类别,其他一个返回子类别..请帮助我解决此查询

ex

i have table called category fields categoryid,category,parentcategoryid.
and this is self refernce table.
if parentcategoryid = 0 that means it''s parent category and if parent category is not 0 that means it is child category..
i want 2 fields in select query which one return parent category only and other one return child category.. please help me to solve this query

ex

SELECT     CategoryId, Category, ParentCategoryId
FROM         Category

推荐答案

此查询将为您提供所有具有父项的类别以及父母的名字.
This query will get you all Categories that have a parent along with the name of the parent.
SELECT  C.Category AS 'Category', 
        C2.Category As 'Parent'
  FROM  Category C
           INNER JOIN Category C2 ON C2.CategoryId = C.ParentCategoryId
 WHERE  C.ParentCategoryId > 0


这篇关于sql查询自引用表查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆