日期:2014-05-17  浏览次数:20627 次

根据角色类型查询不同的表,有时间的帮忙看下~3Q
当前用户编号user_id=1,首先要从accounts_userroles中查询根据user_id查询出角色类型role_type,如果role_type=1则从项目表project中查询所有项目编号,如果为2则从project2中根据user_id查询出项目编号,有没有方法不通过存储过程一次性将数据查出来的?

------解决方案--------------------
SQL code

declare @role_type int
select @role_type=role_type from accounts_userroles where user_id=1
if @role_type=1
begin
 select * from project
end
else
begin
 select * from project2 where userid=1
end

------解决方案--------------------
SQL code
DECLARE @i INT
SET @i = 1  --可以修改为2

SELECT  *
FROM    ( SELECT    * ,
                    1 AS role_type
          FROM      project
          UNION ALL
          SELECT    project2.* ,
                    2 AS role_type
          FROM      project2
          INNER JOIN accounts_userroles C ON C.USER_ID = project2.USER_id
        ) B
        INNER JOIN accounts_userroles A ON A.role_type = B.role_type
WHERE   A.user_id = @i