| categories | | profiles | | categories_map |
--------------- ------------- ----------------------------
| ID | name | | ID | name | | ID | profile_id | cat_id |
--------------- ------------- ----------------------------
| 1 | cat1 | | 1 | p1 | | 1 | 1 | 1 |
| 2 | cat2 | | 2 | p2 | | 2 | 2 | 1 |
| 3 | cat3 | | 3 | p3 | | 3 | 3 | 1 |
| 4 | p4 | | 4 | 1 | 2 |
| 5 | 3 | 2 |
| 6 | 4 | 3 |
| profiles_map |
-----------------------------
| ID | profile_id | acc_id |
-----------------------------
| 1 | 1 | 1 |
| 2 | 3 | 1 |
| 3 | 4 | 1 |
을
계정에 할당 된 카테고리를 가져와야합니다. 즉, acc_id = 1의 카테고리를 가져 오려면 ID가 2와 3 인 카테고리를 가져야합니다. (ID가 2 인 카테고리는 ID가 2 인 프로필이 포함되어있어 적합하지 않습니다. 이 계정에는 할당되지 않음). 이 쿼리를 시도했지만 작동하지 않습니다.
select cats.id from profiles_map map
right join categories_map catm on catm.profile_id = map.profile_id
right join categories cats on cats.id = catm.cat_id
where catm.profile_id in (select profile_id from profiles_map where acc_id = 1)
and map.acc_id = 1 group by cats.id;
아무도이 질문에 도움을 줄 수 있습니까?
:
또는 사용 다음 쿼리, 다음 카테고리 이름과 ID를 얻으려면 이 계정에 할당되지 않은 프로필이있는 범주 찾기 – baskax
스키마 또는 데이터 자체에 문제가 있어야합니다. – KaeL