2

我有下表包含应用程序功能列表:

应用功能

Id Name MenuId

值为:

1 PatientSearch2

应用程序菜单

Id DisplayText Url ParentMenuId

值为

1 Patient patient NULL

2 PatientSearch search1

我需要获取菜单信息以及功能。到目前为止,我只能获得映射菜单,但不能获得父菜单项。

SELECT
AF.Name, AF.Id As FeatureId, AM.Id AS MenuId, AM.DisplayText,AM.Url
FROM 
ApplicationFeature AF 
INNER JOIN ApplicationMeny AM ON AF.MenuId=AM.Id 

我的结果仅显示 PatientSearch 菜单详细信息,但不包括 Patient 菜单。

如何使查询也检索父菜单详细信息?

编辑:

我需要与父母一起建立菜单。如果父菜单有另一个父菜单,我也需要包含该菜单。

-- Parent(Id=2)

  --- SubParent(Id=10 & ParentId=2)

    ----ActualMenuMappedwithFeature(Id=30 & ParentId=10)

在上述情况下,我想要带有菜单 ID 的结果30, 10, 2

4

2 回答 2

1

那么很难说,你想得到什么。但是如果你只想获取一个父母,你可以做额外的加入

select
    AF.Name, AF.Id As FeatureId,
    AM.Id as MenuId, AM.DisplayText, AM.Url,
    AMP.Id as ParentMenuId, AMP.DisplayText as ParentDisplayText, AM.Url as ParentUrl
from ApplicationFeature as AF
    inner join ApplicationMenu as AM on AM.Id = AF.MenuId
    left outer join ApplicationMenu as AMP on AMP.Id = AM.ParentMenuId

如果您想让所有父母都达到顶峰,请澄清所需的输出格式
编辑

declare @FeatureId = ???

;with CTE as (
  select
     AF.Name, AF.Id As FeatureId,
     AM.Id as MenuId, AM.DisplayText, AM.Url,
     AM.ParentId
  from ApplicationFeature as AF
     inner join ApplicationMenu as AM on AM.Id = AF.MenuId
  where AF.Id = @FeatureId

  union all

  select
     null as Name, null as FeatureId,
     AM.Id as MenuId, AM.DisplayText, AM.Url,
     AM.ParentId
  from ApplicationMenu as AM
     inner join CTE as C on C.ParentId = AM.Id
)
select * from CTE
于 2013-08-02T14:05:04.413 回答
-1

更新

您需要第二次加入才能获得父母

SELECT AF.ID,
AM.DisplayText as child,
Parent.DisplayText as parent
FROM ApplicationFeature AF 
LEFT JOIN ApplicationMenu AM ON AF.MenuId = AM.Id 
LEFT JOIN ApplicationMenu as Parent on AM.ParentMenuId = Parent.ID

SQLFiddle

结果

在此处输入图像描述

于 2013-08-02T13:41:40.430 回答