MySQL join with where 절
참석하고 싶은 테이블이 두 개 있어요.
카테고리 테이블의 모든 카테고리와 category_subscriptions 테이블의 사용자가 서브스크라이브하는 모든 카테고리를 원합니다.
기본적으로 지금까지의 질문입니다.
SELECT *
FROM categories
LEFT JOIN user_category_subscriptions
ON user_category_subscriptions.category_id = categories.category_id
이것은 정상적으로 동작합니다만, 쿼리 끝에 where 절을 추가하여 기본적으로 내부/equi join이 되도록 하겠습니다.
SELECT *
FROM categories
LEFT JOIN user_category_subscriptions
ON user_category_subscriptions.category_id = categories.category_id
WHERE user_category_subscriptions.user_id = 1
1개의 쿼리만 사용하여 특정 사용자가 구독하는 모든 카테고리와 모든 카테고리를 가져오려면 어떻게 해야 합니까?
category_id는 카테고리 테이블과 user_category_replications의 양쪽 키입니다.user_category_replications 테이블에 있는 user_id.
감사해요.
여기에 넣으셔야 합니다.join절이 아니라where:
SELECT *
FROM categories
LEFT JOIN user_category_subscriptions ON
user_category_subscriptions.category_id = categories.category_id
and user_category_subscriptions.user_id =1
보세요.inner join명령어를 삽입하다join또는where등가입니다.단, 의 경우outer join, 그들은 매우 다르다.
로서joincondition은 테이블에 결합할 행 집합을 지정합니다.이것은 그것이 평가된다는 것을 의미한다.user_id = 1첫 번째, 서브셋을 취합니다.user_category_subscriptions와 함께user_id의1모든 행에 연결하다categories. 그러면 의 모든 행이 표시됩니다.categories단,categories이 특정 사용자가 서브스크라이브한 정보는user_category_subscriptions컬럼을 클릭합니다.물론, 다른 모든 것들은categories로 채워진다.null에서user_category_subscriptions컬럼을 클릭합니다.
반대로, a는where절은 조인한 후 행 집합을 줄입니다.이렇게 하면 모든 결합이 수행되고 모든 행이 제거됩니다.user_id같지 않다1비능률적인 방법으로 데이터를 얻을 수 있습니다.inner join.
이게 도움이 됐으면 좋겠네요!
이거 드셔보세요
SELECT *
FROM categories
LEFT JOIN user_category_subscriptions
ON user_category_subscriptions.category_id = categories.category_id
WHERE user_category_subscriptions.user_id = 1
or user_category_subscriptions.user_id is null
언급URL : https://stackoverflow.com/questions/1219909/mysql-join-with-where-clause
'source' 카테고리의 다른 글
| PHP는 키를 유지하면서 연관 배열의 마지막 3개 요소를 가져옵니다. (0) | 2022.11.11 |
|---|---|
| PHP 오류: "zip 확장 및 unzip 명령이 모두 없습니다. 건너뜁니다." (0) | 2022.11.11 |
| Array IE8에서 Index Of가 작동하지 않는 이유는 무엇입니까? (0) | 2022.11.11 |
| MySQL에 있는 테이블 수를 세는 쿼리 (0) | 2022.11.11 |
| SQL 피벗 및 문자열로 저장된 날짜 간의 차이 계산 - MySQL (0) | 2022.11.11 |