+ -
当前位置:首页 → 问答吧 → 行转列的语句要怎么写,请帮忙

行转列的语句要怎么写,请帮忙

时间:2011-10-21

来源:互联网

我有一个表role和和一个role_user,还一个user表
role表中有
role_id role_name
1 增加
2 修改
3 删除
表role_user中有
role_id user_id
1 001
2 001
1 002
1 003
2 003
3 003

user表中
user_id user_name
001 张三
002 李四
003 王五


现在要的结果是这样的:
增加 修改 删除  
张三 张三 王五
李四 王五
王五

作者: bota   发布时间: 2011-10-21

SQL code

select case when role_name = '增加' then user_name end as "增加",
       case when role_name = '修改' then user_name end as "修改",
       case when role_name = '删除' then user_name end as "删除"
  from (select user_name, role_name
          from role_tab a, role_user b, user_tab c
         where a.role_id = b.role_id
           and c.user_id = b.user_id);



作者: opps_zhou   发布时间: 2011-10-21

我这个是示例,实际的权限表有100多个权限,写死不行啊

作者: bota   发布时间: 2011-10-21

SQL code
select  
max(case when c.role_id=1 then a.user_name end) 增加,
max(case when c.role_id=2 then a.user_name end) 删除,
max(case when c.role_id=3 then a.user_name end)  修改
 from 
[user] a,role_use b,role c
where a.user_id=b.user_id and b.role_id=c.role_id

作者: cosio   发布时间: 2011-10-21

那就是写成函数!

作者: cosio   发布时间: 2011-10-21

需要怎么写,没写过函数,汗

作者: bota   发布时间: 2011-10-21

哪位大虾帮忙写个,搞不出来啊

作者: bota   发布时间: 2011-10-21

有没有哪位大虾帮忙啊,急啊

作者: bota   发布时间: 2011-10-21

热门下载

更多