Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

สร้าง MySQL View ด้วย RIGHT JOIN

การสร้าง View ด้วย RIGHT JOIN

ใช้ตาราง Customers และ Reserve:

mysql> SELECT * FROM Customers;
+-------------+----------+
| Customer_Id | Name     |
+-------------+----------+
|           1 | Rahul    |
|           2 | Yashpal  |
|           3 | Gaurav   |
|           4 | Virender |
+-------------+----------+

mysql> SELECT * FROM Reserve;
+------+------------+
| ID   | Day        |
+------+------------+
|    1 | 2017-12-30 |
|    2 | 2017-12-28 |
|    2 | 2017-12-25 |
|    1 | 2017-12-24 |
|    3 | 2017-12-26 |
+------+------------+

สร้าง View customer_VRight (ลูกค้าที่ไม่ได้จอง)

mysql> CREATE VIEW customer_VRight AS 
    -> SELECT Name FROM Reserve 
    -> RIGHT JOIN Customers ON Customer_Id = ID 
    -> WHERE ID IS NULL;
Query OK, 0 rows affected (0.08 sec)

mysql> SELECT * FROM customer_VRight;
+----------+
| Name     |
+----------+
| Virender |
+----------+

RIGHT JOIN คืนทุกแถวจาก Customers (ตารางขวา) แม้ไม่มีคู่ใน Reserve Virender ไม่มีการจอง จึงแสดง