2009-05-14 7 views
1
mysql> select * from product; 
+------------+---------------+ 
| product_id | name   | 
+------------+---------------+ 
|   1 | Car   | 
|   2 | House   | 
|   3 | Cat   | 
|   4 | Blank Product | 
+------------+---------------+ 
4 rows in set (0.00 sec) 

mysql> select * from tag; 
+--------+-----------+ 
| tag_id | name  | 
+--------+-----------+ 
|  1 | Expensive | 
|  2 | Fast  | 
|  3 | Mean  | 
|  4 | Large  | 
|  5 | Small  | 
|  6 | Alive  | 
|  7 | Blank Tag | 
+--------+-----------+ 
7 rows in set (0.00 sec) 

mysql> select * from product_tag; 
+------------+--------+ 
| product_id | tag_id | 
+------------+--------+ 
|   1 |  1 | 
|   1 |  2 | 
|   1 |  3 | 
|   1 |  4 | 
|   2 |  1 | 
|   2 |  4 | 
|   3 |  2 | 
|   3 |  3 | 
|   3 |  5 | 
|   3 |  6 | 
+------------+--------+ 
10 rows in set (0.00 sec) 

다음 쿼리는 내 빈 태그를 반환하지만 내 제품은 반환하지 않는 이유는 무엇입니까?MySQL 다중 오른쪽 조인

mysql> select * from product_tag right join product using (product_id) 
           right join tag using (tag_id); 
+--------+-----------+------------+-------+ 
| tag_id | name  | product_id | name | 
+--------+-----------+------------+-------+ 
|  1 | Expensive |   1 | Car | 
|  1 | Expensive |   2 | House | 
|  2 | Fast  |   1 | Car | 
|  2 | Fast  |   3 | Cat | 
|  3 | Mean  |   1 | Car | 
|  3 | Mean  |   3 | Cat | 
|  4 | Large  |   1 | Car | 
|  4 | Large  |   2 | House | 
|  5 | Small  |   3 | Cat | 
|  6 | Alive  |   3 | Cat | 
|  7 | Blank Tag |  NULL | NULL | 
+--------+-----------+------------+-------+ 
11 rows in set (0.00 sec) 

답변

2

제품 ID 4와 태그를 연결하는 행이 없습니다. 다음과 같이 product_tag 테이블에 행을 추가해야합니다.

+------------+--------+ 
| product_id | tag_id | 
+------------+--------+ 
|   4 |  7 | 
+------------+--------+ 
+0

태그 7 (빈 태그)을 제품에 연결하는 행이 없지만 나의 오른쪽 조인이이를 리턴합니다. –

4

오른쪽 조인을 사용하고 있습니다. 쿼리 태그에서 ids는 MySQL이 일치를 시작할 기준입니다. 오른쪽 조인은 오른쪽에서 왼쪽으로 평가됩니다. 쿼리를 두 부분으로 나누면. 첫 번째는 다음과 같습니다

select * from product_tag right join tag using (tag_id); 
+--------+-----------+------------+ 
| tag_id | name  | product_id | 
+--------+-----------+------------+ 
|  1 | expensive |   1 | 
|  1 | expensive |   2 | 
|  2 | fast  |   1 | 
|  2 | fast  |   3 | 
|  3 | mean  |   1 | 
|  3 | mean  |   3 | 
|  4 | larg  |   1 | 
|  4 | larg  |   2 | 
|  5 | small  |   3 | 
|  6 | alive  |   3 | 
|  7 | blank tag |  NULL | 

+--------+-----------+------------+ 

당신이 빈 태그와 일치 PRODUCT_ID이없는시피. 왜이 결과를 제품 테이블에 결합하면 왜 결과를 얻을 수 있는지 설명합니다.

select * from product_tag left join product using (product_id) left join tag using (tag_id); 

+--------+------------+-------+-----------+ 
| tag_id | product_id | name | name  | 
+--------+------------+-------+-----------+ 
|  1 |   1 | car | expensive | 
|  2 |   1 | car | fast  | 
|  3 |   1 | car | mean  | 
|  4 |   1 | car | larg  | 
|  1 |   2 | house | expensive | 
|  4 |   2 | house | larg  | 
|  2 |   3 | cat | fast  | 
|  3 |   3 | cat | mean  | 
|  5 |   3 | cat | small  | 
|  6 |   3 | cat | alive  | 
+--------+------------+-------+-----------+ 
+0

좋은 답변입니다. 매우 상세한 +1 –

0

에는이 경우 당신은 오른쪽 테이블에 그렇게 모든 행은 "태그"도 반환됩니다 즉, 가입 권리를 사용하지 않습니다 :

당신이 왼쪽 사용하는 경우

는이 결과를 얻을 수 있습니다 대신 가입 조인 된 테이블에서 일치.