使用SQL显示子类别
好吧,这让人困惑,所以我决定写一个新问题 我需要检查类别表中的一行是否为子类别。然后我需要检查孩子属于哪个父类别 父类别的categoryID和ParentCategoryID为0 子类别有categoryID和ParentCategoryID,如30 类别表:使用SQL显示子类别,sql,sql-server-2008,nopcommerce,Sql,Sql Server 2008,Nopcommerce,好吧,这让人困惑,所以我决定写一个新问题 我需要检查类别表中的一行是否为子类别。然后我需要检查孩子属于哪个父类别 父类别的categoryID和ParentCategoryID为0 子类别有categoryID和ParentCategoryID,如30 类别表: ID PCID NAME 10 0 Computers 11 10 Software 12 10 Hardware 这就是我所尝试的: ID PCID NAME 10 0 Computers 11 10 Sof
ID PCID NAME
10 0 Computers
11 10 Software
12 10 Hardware
这就是我所尝试的:
ID PCID NAME
10 0 Computers
11 10 Software
12 10 Hardware
这将显示父类别(因为PCID 0是父类别):
自动连接回表以查找子级的实际父级
SELECT c1.CategoryID, c2.ParentCategoryID, c1.Name, c2.Name as ParentName, c1.Published, c1.Deleted, c1.PictureID
FROM Nop_Category c1
JOIN Nop_Category c2 on c1.ParentCategoryId = c2.CategoryId
WHERE (c1.Deleted = 0)
AND (c1.Published = 1)
AND (c1.ParentCategoryID = 10)
这将返回“计算机”类别的两个子项。这就是你要找的吗
当然,您可以改变此设置以显示特定父级或所有父级的所有子级:
SELECT c.*, p.* -- shortened, but you should pick specific columns
FROM Nop_Category p -- parent
JOIN Nop_Category c ON c.ParentCategoryId = p.CategoryId -- children
WHERE p.ParentCategoryId = 0 -- all top level parents
SELECT c.*, p.* -- shortened, but you should pick specific columns
FROM Nop_Category p -- parent
JOIN Nop_Category c ON c.ParentCategoryId = p.CategoryId -- children
WHERE p.CategoryId = 10 -- only the "Computers" category
或者,如果您只想要“计算机”类别的子类别,请将ParentCategoryId更改为10
SELECT CategoryID, ParentCategoryID, Name, Published, Deleted, PictureID
FROM Nop_Category
WHERE (Deleted = 0)
AND (Published = 1)
AND (ParentCategoryID = 10)
自动连接回表以查找子级的实际父级
SELECT c1.CategoryID, c2.ParentCategoryID, c1.Name, c2.Name as ParentName, c1.Published, c1.Deleted, c1.PictureID
FROM Nop_Category c1
JOIN Nop_Category c2 on c1.ParentCategoryId = c2.CategoryId
WHERE (c1.Deleted = 0)
AND (c1.Published = 1)
AND (c1.ParentCategoryID = 10)
这将返回“计算机”类别的两个子项。这就是你要找的吗
当然,您可以改变此设置以显示特定父级或所有父级的所有子级:
SELECT c.*, p.* -- shortened, but you should pick specific columns
FROM Nop_Category p -- parent
JOIN Nop_Category c ON c.ParentCategoryId = p.CategoryId -- children
WHERE p.ParentCategoryId = 0 -- all top level parents
SELECT c.*, p.* -- shortened, but you should pick specific columns
FROM Nop_Category p -- parent
JOIN Nop_Category c ON c.ParentCategoryId = p.CategoryId -- children
WHERE p.CategoryId = 10 -- only the "Computers" category
或者,如果您只想要“计算机”类别的子类别,请将ParentCategoryId更改为10
SELECT CategoryID, ParentCategoryID, Name, Published, Deleted, PictureID
FROM Nop_Category
WHERE (Deleted = 0)
AND (Published = 1)
AND (ParentCategoryID = 10)
您的查询应返回1条记录“Computers”。那么,您到底在寻找什么呢?希望返回子类别,以便我可以在中继器控件中使用它们。因此,当用户将鼠标悬停在父项上时,会显示子项。好的,然后查看我答案的底部-您希望ParentCategoryID=10您的查询应返回1条记录“Computers”。那么,您到底在寻找什么呢?希望返回子类别,以便我可以在中继器控件中使用它们。因此,当用户将鼠标悬停在父项上时,会显示子项。好的,然后查看我答案的底部-您希望ParentCategoryID=10我是否可以执行ParentCategoryID>0请注意,我需要传递一个值来确定ParentCategoryID,以便显示子类别。亲爱的,您在周末指导了我。这个世界上应该有更多像你这样的人。非常感谢!:)一个返回父级然后返回子级、父级然后返回子级的查询如何。或者,考虑到我当前的设置,这是不可能的?实际上,也可以将其划掉,当您运行基本的select查询时,系统似乎已经这样分组了。我是否可以执行ParentCategoryID>0划掉,我需要传递一个值来确定ParentCategoryID,以便显示子类别。sweet,你在周末指导了我。这个世界上应该有更多像你这样的人。非常感谢!:)一个返回父级然后返回子级、父级然后返回子级的查询如何。或者,考虑到我目前的设置,这是不可能的?实际上,这也需要重新考虑一下,当运行基本的select查询时,系统似乎已经这样分组了。