日期:2014-05-18  浏览次数:20578 次

sql 从三张表中查询并合并一张表
有表Permission
  id Name TypeCode GroupId
  1 编辑 0 1
  2 浏览 0 1
  3 派发 0 1
  4 系统管理 0 5
  5 编辑 0 2

表PermissionGroup
  id Name  
  1 线索
  2 选题
  5 系统管理

表RolePermission
 RoleDefId PermissionId
  3 1
  3 2
  3 3
  3 4

现在是根据RoleDefId(不符合这个ID的),选出表Permission和PermissionGroup中的信息,而Name要合并成Permission.Name(PermissionGroup.Name),我写的sql如下


SQL code

ALTER PROCEDURE [dbo].[Proc_GetRolePEsRID]
(
    @RoleId int
)
AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
        
        select * from (select T2.Id,T2.TypeCode,T2.GroupId,T1.Id as GId,T2.Name+'('+T1.Name+')' as Name from ( select * from [PermissionGroup])as T1,( select * from [Permission]) as T2 where T1.Id = T2.GroupId)as T3 where T3.Id in (select PermissionId from [RolePermission] where not RolePermission.RoleDefId = @RoleId)

    END


查询出来的结果,都只有编辑(线索)等等(线索)的,有个选题的却显示不出来,为什么呢?

------解决方案--------------------
就如上数据,结果是怎样的。