我有以下结构
user
id | name
----------
1 | Foo
2 | Bar
profile
id | name | user_id
--------------------------
1 | Profile 1 | 1
2 | Profile 2 | 2
profile_access
id | user_id | profile_id
--------------------------
1 | 2 | 1
期望从查询中获得以下结果
id | name | user_id
-----------------------------------
1 | Profile 1 | 1
2 | Profile 2 | 2
1 | Profile 1 | 2
但我不知道如何“合并”这些表格。我尝试过:
SELECT profile.*
FROM profile profile
LEFT JOIN profile_access AS profile_access
ON (
profile_access.profile_id = profile.id
)
这个返回值
id | name | user_id
-----------------------------------
1 | Profile 1 | 1
2 | Profile 2 | 2
并且
SELECT profile.*
FROM profile profile
RIGHT JOIN profile_access AS profile_access
ON (
profile_access.profile_id = profile.id
)
哪些结果
id | name | user_id
-----------------------------------
2 | Profile 1 | 2
什么是正确的查询方式?我是否在使用联接方面有误或者期望这些表格的结果是不可能的?
编辑: 预期的结果应该是:
id | name | user_id
-----------------------------------
1 | Profile 1 | 1
2 | Profile 2 | 2
1 | Profile 1 | 2