MySQL - RIGHT JOIN

임재현·2021년 5월 15일
0

MySQL

목록 보기
48/52

Udemy - The Ultimate MySQL Bootcamp: Go from SQL Beginner to Expert를 수강하며 정리하는 글

RIGHT JOIN

mysql> SELECT * FROM customers
    -> LEFT JOIN orders
    -> ON customers.id = orders.customer_id;
+----+------------+-----------+-----------------+------+------------+--------+-------------+
| id | first_name | last_name | email           | id   | order_date | amount | customer_id |
+----+------------+-----------+-----------------+------+------------+--------+-------------+
|  2 | George     | Michael   | gm@gmail.com    |    3 | 2014-12-12 | 800.67 |           2 |
|  2 | George     | Michael   | gm@gmail.com    |    4 | 2015-01-03 |  12.50 |           2 |
|  3 | David      | Bowie     | david@gmail.com | NULL | NULL       |   NULL |        NULL |
|  4 | Blue       | Steele    | blue@gmail.com  | NULL | NULL       |   NULL |        NULL |
|  5 | Bette      | Davis     | bette@aol.com   |    5 | 1999-04-11 | 450.25 |           5 |
+----+------------+-----------+-----------------+------+------------+--------+-------------+
5 rows in set (0.00 sec)


mysql> SELECT * FROM orders
    -> RIGHT JOIN customers
    -> ON orders.customer_id = customers.id;
+------+------------+--------+-------------+----+------------+-----------+-----------------+
| id   | order_date | amount | customer_id | id | first_name | last_name | email           |
+------+------------+--------+-------------+----+------------+-----------+-----------------+
|    3 | 2014-12-12 | 800.67 |           2 |  2 | George     | Michael   | gm@gmail.com    |
|    4 | 2015-01-03 |  12.50 |           2 |  2 | George     | Michael   | gm@gmail.com    |
| NULL | NULL       |   NULL |        NULL |  3 | David      | Bowie     | david@gmail.com |
| NULL | NULL       |   NULL |        NULL |  4 | Blue       | Steele    | blue@gmail.com  |
|    5 | 1999-04-11 | 450.25 |           5 |  5 | Bette      | Davis     | bette@aol.com   |
+------+------------+--------+-------------+----+------------+-----------+-----------------+
5 rows in set (0.00 sec)

LEFT JOIN과 RIGHT JOIN은 사실 차이가 없다. 위 처럼 같은 데이터가 나오게 할 수 있다.(데이터가 나온 순서는 다르다. 하지만 같은 데이터다.)

profile
임재현입니다.

0개의 댓글