返回它们满足sql中特定值的列名

时间:2014-11-27 11:25:42

标签: mysql sql

我的用户表具有"访问角色"在表格中定义。

架构:

UserID, UserName, Access_1, Access_2, Access_3, .... Access_10

此处Access_Columns的值为1或0

目标:我需要用户及其允许的访问权限在一列中作为“访问_允许”#。

UserId, Access_Allowed  
1,     (Access_1, Access_3, Access_4, ...)  
2,     (Access_2, Access_3, Access_5, Access_10, ...)

的问候,
Yugal

1 个答案:

答案 0 :(得分:0)

Thanks Strawberry... I've Used Concat_ws here...  

select userid, username,  
concat_ws(', ',  
IF(Access1 = 1 ,'Access1', NULL),  
IF(Access2 = 1 ,'Access2', NULL),  
IF(Access3 = 1 ,'Access3', NULL),  
IF(Access4 = 1 ,'Access4', NULL),  
.  
.  
IF( AccessN = 1 ,'AccessN',NULL) as Access_Allowed  
from UserTable

Thanks