Programing

MySQL : 조인 유형의 빠른 분석

lottogame 2020. 6. 14. 10:20
반응형

MySQL : 조인 유형의 빠른 분석


MySQL 조인 유형에 대한 빠른 분석을 원합니다. 나는 이것들을 알고 나머지는 그들이 무엇을 의미하는지 잘 모르겠습니다.

  • 쉼표로 구분 (이것이 정확히 무엇 을 의미합니까?) :SELECT * FROM a, b WHERE b.id = a.beeId AND ...
  • b에 일치하는 항목이 없더라도 a의 정보를 표시합니다. SELECT * FROM a LEFT OUTER JOIN b ON b.id = a.beeId WHERE ...

본인은 조인을 볼 수 있지만 다른, 무엇인지하게 알고 싶어 한 INNER/ OUTER추가 않는 LEFT변경 일을.

조인의 작동 방식을 이미 알고 있으며 다른 유형의 조인이 있는지 또는 동일한 결과를 얻는 다른 방법인지 알고 싶습니다.


G +에서 발견
(c) G + "데이터 시각화"에서 발견

또는 좋은 개요를 보려면 다음 링크를 참조하십시오.

http://www.khankennels.com/blog/index.php/archives/2007/04/20/getting-joins/

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html


귀하의 의견을 바탕으로, 각각의 간단한 정의는 최고의 발견 W3 스쿨 각 유형의 첫 번째 줄은 유형 가입에 대한 간략한 설명을 제공합니다

  • JOIN : 두 테이블에 하나 이상의 일치 항목이 있으면 행을 반환합니다.
  • LEFT JOIN : 오른쪽 테이블에 일치하는 항목이 없더라도 왼쪽 테이블에서 모든 행을 반환합니다.
  • RIGHT JOIN : 왼쪽 테이블에 일치하는 항목이 없더라도 오른쪽 테이블의 모든 행을 반환합니다.
  • FULL JOIN : 테이블 중 하나에 일치하는 행을 반환

편집 종료

간단히 말해, 쉼표로 구분 된 예제는

SELECT * FROM a, b WHERE b.id = a.beeId AND ...

테이블을 구분하는 쉼표로 테이블 a와 b에서 모든 레코드를 선택하는 것입니다.

SELECT a.beeName,b.* FROM a, b WHERE b.id = a.beeId AND ...

그런 다음 b.id 열과 a.beeId 열이 예제에서 일치하는 행에 지시 정보를 가져옵니다. 따라서 귀하의 예에서는 b.id가 a.beeId와 같은 테이블 a와 b에서 모든 정보를 얻습니다. 이 예에서는 b.id가 a.beeId와 같을 때 b 테이블의 모든 정보와 a.beeName 열의 정보 만 가져옵니다. AND 절도 있으므로 결과를 구체화하는 데 도움이됩니다.

mySQL 조인 및 왼쪽 조인에 대한 간단한 자습서 및 설명은 Tizag의 mySQL 자습서를 살펴보십시오. 당신은 또한 확인하실 수 있습니다 키스 J. 브라운의 웹 사이트를 그 또한 꽤 좋은 조인에 대한 자세한 내용은.

나는 이것이 당신에게 도움이되기를 바랍니다


전체 외부 조인은 mysql에 존재하지 않으므로 왼쪽 및 오른쪽 조인 조합을 사용해야 할 수도 있습니다.


나는 이와 같은 2 개의 테이블을 가지고있다 :

> SELECT * FROM table_a;
+------+------+
| id   | name |
+------+------+
|    1 | row1 |
|    2 | row2 |
+------+------+

> SELECT * FROM table_b;
+------+------+------+
| id   | name | aid  |
+------+------+------+
|    3 | row3 |    1 |
|    4 | row4 |    1 |
|    5 | row5 | NULL |
+------+------+------+

INNER JOIN cares about both tables

INNER JOIN cares about both tables, so you only get a row if both tables have one. If there is more than one matching pair, you get multiple rows.

> SELECT * FROM table_a a INNER JOIN table_b b ON a.id=b.aid;
+------+------+------+------+------+
| id   | name | id   | name | aid  |
+------+------+------+------+------+
|    1 | row1 |    3 | row3 | 1    |
|    1 | row1 |    4 | row4 | 1    |
+------+------+------+------+------+

It makes no difference to INNER JOIN if you reverse the order, because it cares about both tables:

> SELECT * FROM table_b b INNER JOIN table_a a ON a.id=b.aid;
+------+------+------+------+------+
| id   | name | aid  | id   | name |
+------+------+------+------+------+
|    3 | row3 | 1    |    1 | row1 |
|    4 | row4 | 1    |    1 | row1 |
+------+------+------+------+------+

You get the same rows, but the columns are in a different order because we mentioned the tables in a different order.

LEFT JOIN only cares about the first table

LEFT JOIN cares about the first table you give it, and doesn't care much about the second, so you always get the rows from the first table, even if there is no corresponding row in the second:

> SELECT * FROM table_a a LEFT JOIN table_b b ON a.id=b.aid;
+------+------+------+------+------+
| id   | name | id   | name | aid  |
+------+------+------+------+------+
|    1 | row1 |    3 | row3 | 1    |
|    1 | row1 |    4 | row4 | 1    |
|    2 | row2 | NULL | NULL | NULL |
+------+------+------+------+------+

Above you can see all rows of table_a even though some of them do not match with anything in table b, but not all rows of table_b - only ones that match something in table_a.

If we reverse the order of the tables, LEFT JOIN behaves differently:

> SELECT * FROM table_b b LEFT JOIN table_a a ON a.id=b.aid;
+------+------+------+------+------+
| id   | name | aid  | id   | name |
+------+------+------+------+------+
|    3 | row3 | 1    |    1 | row1 |
|    4 | row4 | 1    |    1 | row1 |
|    5 | row5 | NULL | NULL | NULL |
+------+------+------+------+------+

Now we get all rows of table_b, but only matching rows of table_a.

RIGHT JOIN only cares about the second table

a RIGHT JOIN b gets you exactly the same rows as b LEFT JOIN a. The only difference is the default order of the columns.

> SELECT * FROM table_a a RIGHT JOIN table_b b ON a.id=b.aid;
+------+------+------+------+------+
| id   | name | id   | name | aid  |
+------+------+------+------+------+
|    1 | row1 |    3 | row3 | 1    |
|    1 | row1 |    4 | row4 | 1    |
| NULL | NULL |    5 | row5 | NULL |
+------+------+------+------+------+

This is the same rows as table_b LEFT JOIN table_a, which we saw in the LEFT JOIN section.

Similarly:

> SELECT * FROM table_b b RIGHT JOIN table_a a ON a.id=b.aid;
+------+------+------+------+------+
| id   | name | aid  | id   | name |
+------+------+------+------+------+
|    3 | row3 | 1    |    1 | row1 |
|    4 | row4 | 1    |    1 | row1 |
| NULL | NULL | NULL |    2 | row2 |
+------+------+------+------+------+

Is the same rows as table_a LEFT JOIN table_b.

No join at all gives you copies of everything

If you write your tables with no JOIN clause at all, just separated by commas, you get every row of the first table written next to every row of the second table, in every possible combination:

> SELECT * FROM table_b b, table_a;
+------+------+------+------+------+
| id   | name | aid  | id   | name |
+------+------+------+------+------+
|    3 | row3 | 1    |    1 | row1 |
|    3 | row3 | 1    |    2 | row2 |
|    4 | row4 | 1    |    1 | row1 |
|    4 | row4 | 1    |    2 | row2 |
|    5 | row5 | NULL |    1 | row1 |
|    5 | row5 | NULL |    2 | row2 |
+------+------+------+------+------+

(This is from my blog post Examples of SQL join types)

참고 URL : https://stackoverflow.com/questions/6294778/mysql-quick-breakdown-of-the-types-of-joins

반응형