第三个选择节目显示前两个选择的重复结果。 所以我说我的数据集只有5个ID。 前两个语句生成id 1,2,3 enabled = 1 我需要的最后一个语句ids4,5 enabled = 0
但是现在我得到id 1,2,3,4,5启用= 1 PLUS ID 1,2,3,4,5 enabled = 0
SELECT p.*, 1 as enabled
FROM member_permissions mp
JOIN permissions p ON p.permission_id = mp.permission_id
WHERE member_id = 1
UNION
SELECT pgp.*, 1 as enabled
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
SELECT *, 0 as enabled
FROM permissions p
答案 0 :(得分:0)
不幸的是,您必须执行两次顶级查询才能从较低的查询中删除它们:
FriendsOfCake/cakephp-csvview
答案 1 :(得分:0)
在这种情况下,您可以尝试使用左连接。
SELECT DISTINCT
prm.*,
CASE
WHEN mp.member_id IS NULL OR mp2.member_id IS NULL THEN 0
ELSE 1
END AS ENABLED
FROM permissions prm
LEFT JOIN member_permissions mp
ON mp.permission_id = prm.permission_id
AND mp.member_id = 1
LEFT JOIN permission_link pl
ON pl.permission_group_id = prm.permission_group_id
LEFT JOIN member_permissions mp2
ON mp2.permission_group_id = pl.permission_group_id
AND mp2.member_id = 1
不是我认为的最佳解决方案,但可能有效