Mysql:如何在UNION之前添加虚拟列

时间:2016-10-03 03:01:23

标签: mysql

我想为我的语句的第一个联合的所有结果添加一个虚拟列'enabled' = 1,为下一个联合添加'enabled' = 0。这是我到目前为止的陈述......

SELECT p.*
FROM member_permissions mp
JOIN permissions p ON p.permission_id = mp.permission_id
WHERE member_id = 1
UNION
SELECT pgp
FROM member_permissions mp
JOIN permission_link pl ON pl.permission_group_id = mp.permission_group_id
JOIN permissions pgp ON pgp.permission_id = pl.permission_id
WHERE member_id = 1
UNION        // this will have enabled = 0 //    
SELECT *
FROM permissions        

1 个答案:

答案 0 :(得分:3)

只需将新列指定为源:

select column1, column2, ..., 1 as enabled
from ....
union
select column1, column2, ..., 0 as enabled
from ....

请注意,使用union时,必须使用具有相同数据类型的相同数量的列。