连续加入同一个表中的2个字段

时间:2014-11-15 18:45:02

标签: mysql

我有这个:

id     Level      module        permissions
------------------------------------------------
15    level 15    module 1    edit, add, delete
15    level 15    module 2    add, report

我想得到像这样的结果:

id     Level      module        permissions        module      permissions 
---------------------------------------------------------------------------
15    level 15    module 1    edit, add, delete   module 2     add, report

我该怎么办?

1 个答案:

答案 0 :(得分:0)

SELECT id, level, 
       'module 1' as module1, max(case when module = 'module 1' then permissions end) as permissions1,
       'module 2' as module2, max(case when module = 'module 2' then permissions end) as permissions2
from your_table
group by id, level