บทความนี้สอนวิธีการ JOIN ตาราง SQL 4 ตาราง โดยใช้คำสั่ง JOIN หรือ INNER JOIN โดยจะแสดงเฉพาะข้อมูลที่มีความสัมพันธ์กันผ่าน primary key และ foreign key เราจะได้เรียนรู้ตัวอย่างการเขียน SQL เพื่อ Join 4 ตาราง พร้อมโครงสร้างตาราง และคำอธิบาย SQL โดยมีรายละเอียดดังนี้
ตัวอย่าง โครงสร้างตาราง
1. ตาราง orders เก็บข้อมูลคำสั่งซื้อ มี id เป็น primary key
+----+---------------------+-------------+
| id | created             | paid_status |
+----+---------------------+-------------+
|  1 | 2024-01-29 08:03:17 | paid        |
|  2 | 2024-01-29 11:03:17 | paid        |
|  5 | 2024-01-30 14:03:17 | unpaid      |
+----+---------------------+-------------+
2. ตาราง order_details เก็บข้อมูลรายละเอียดคำสั่งซื้อ มี id เป็น primary key และ order_id, product_id เป็น foreign key
+----+------------+--------+----------+
| id | product_id | amount | order_id |
+----+------------+--------+----------+
|  1 |          1 |      2 |        1 |
|  2 |          3 |      2 |        1 |
|  3 |          2 |      1 |        2 |
+----+------------+--------+----------+
3. ตาราง products เก็บข้อมูลสินค้า มี id เป็น primary key และ category_id เป็น foreign key
+----+--------------+--------+-------------+
| id | title        | price  | category_id |
+----+--------------+--------+-------------+
|  1 | Computer     | 200.00 |           1 |
|  2 | Notebook     | 180.00 |           1 |
|  3 | Mobile phone | 100.00 |           2 |
+----+--------------+--------+-------------+
4. ตาราง categorys เก็บประเภทสินค้า มี id เป็น primary key
+----+-----------+
| id | title     |
+----+-----------+
|  1 | Computer  |
|  2 | Gadget    |
|  3 | Accessory |
|  4 | Battery   |
+----+-----------+
ตัวอย่าง วิธีการ JOIN ตาราง SQL 4 ตาราง
SELECT o.id, o.created, o.paid_status, od.amount, p.title, p.price, c.title 
FROM orders o
JOIN order_details od ON ( o.id = od.order_id )
JOIN products p ON ( od.product_id = p.id )
JOIN categorys c ON ( p.category_id = c.id )ผลลัพธ์
+----+---------------------+-------------+--------+--------------+--------+----------+
| id | created             | paid_status | amount | title        | price  | title    |
+----+---------------------+-------------+--------+--------------+--------+----------+
|  1 | 2024-01-29 08:03:17 | paid        |      2 | Computer     | 200.00 | Computer |
|  1 | 2024-01-29 08:03:17 | paid        |      2 | Mobile phone | 100.00 | Gadget   |
|  2 | 2024-01-29 11:03:17 | paid        |      1 | Notebook     | 180.00 | Computer |
+----+---------------------+-------------+--------+--------------+--------+----------+จากคำสั่ง SQL สำหรับการ Join ตาราง 4 ตารางพร้อมกัน สามารถอธิบายได้ดังนี้
1. SELECT ตามด้วยชื่อ field ที่ต้องการแสดง ประกอบด้วย id, created, paid_status, amount, title (products), price และ title (categorys)
2. FROM orders o คือ กำหนดให้ตารางแรก คือ orders ตรง o คือกำหนดชื่อย่อเพื่อหลีกเลี่ยงการซ้ำกันของชื่อ field
3. JOIN order_details od ON ( o.id = od.order_id ) คือ Join ระหว่างตาราง orders กับ orders_detail ผ่าน field o.id ต้องเท่ากับ od.order_id
4. JOIN products p ON ( od.product_id = p.id ) คือ Join ระหว่างตาราง order_details กับ products ผ่าน field od.product_id ต้องเท่ากับ p.id
5. JOIN categorys c ON ( p.category_id = c.id ) คือ Join ระหว่างตาราง products กับ categorys ผ่าน field p.category_id ต้องเท่ากับ c.id