คำสั่ง sql Select ข้อมูลสองตาราง Mysql ใช้ได้ กับ Sql มาตรฐาน ใช้การ Joins Left Join, Right Join, Inner Join

ตัวอย่างมีตารางข้อมูล
ตาราง products;
+----+--------------+--------------+
| id | product_name | manufacturer |
+----+--------------+--------------+
| 1 | Shoes | Company1 |
| 2 | Laptop | Company2 |
| 3 | Monitor | Company3 |
| 4 | DVD | Company4 |
+----+--------------+--------------+

ตาราง buyers;
+----+------+------------+----------+
| id | pid | buyer_name | quantity |
+----+------+------------+----------+
| 1 | 1 | Steve | 2 |
| 2 | 2 | John | 1 |
| 3 | 3 | Larry | 1 |
| 4 | 3 | Michael | 5 |
| 5 | NULL | Steven | NULL |
+----+------+------------+----------+
การ Join ตารางแบบ Left Join เป็นการค้นหาข้อมูลจากสองตารางโดยยึดตาราง ทางซ้ายเป็นหลัก ในที่นี่ตารางซ้ายคือ buyers ต้องมีข้อมูล
SELECT buyer_name, quantity, product_name FROM buyers LEFT JOIN products ON
buyers.pid=products.id;
ผล
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve | 2 | Shoes |
| John | 1 | Laptop |
| Larry | 1 | Monitor |
| Michael | 5 | Monitor |
| Steven | NULL | NULL |
+------------+----------+--------------+
การ Join ตาราง แบบ Right Join
เป็นการค้นหาข้อมูลจากสองตารางโดยยึดตาราง ทางซ้ายเป็นหลัก ในที่นี่ตารางขวาคือ products ต้องมีข้อมูล
SELECT buyer_name, quantity, product_name FROM buyers RIGHT JOIN products ON 
buyers.pid=products.id;

+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
| Steve | 2 | Shoes |
| John | 1 | Laptop |
| Larry | 1 | Monitor |
| Michael | 5 | Monitor |
| NULL | NULL | DVD |
+------------+----------+--------------+
การ Join ตารางแบบ Inner join ข้อมูลที่จะออกมาต้องมีทั้งในตาราง ซ้ายและขวา เครื่องหมาย= (buyers.pid=products.id)
SELECT buyer_name, quantity, product_name FROM buyers INNER JOIN products ON
buyers.pid=products.id;
ลองเอาไปรันดูผล
สำหรับตารางข้อมูล มากกว่า สองตารางก็ใช้หลักการเดียวกัน
กระทู้ล่าสุดจากเว็บบอร์ด
หัวข้อกระทู้
ตอบ
เปิดดู
ล่าสุด
ทำยังเราจะใช้งาน BBcode ที่เรา add เข้าไปได้
โดย thatsawan จ 01 ธ.ค. 2014 1:12 am บอร์ด Programming - PHP
6
924
จ 01 ธ.ค. 2014 1:43 am โดย thatsawan View Topic ทำยังเราจะใช้งาน BBcode ที่เรา add เข้าไปได้
phpbb3.1 ใช้ Symfony เวอชั่นต่ำกว่าหรือสูงกว่า 2.0.15 คับ
โดย offing จ 01 ธ.ค. 2014 12:41 am บอร์ด Programming - PHP
1
1096
จ 01 ธ.ค. 2014 12:43 am โดย thatsawan View Topic phpbb3.1 ใช้ Symfony เวอชั่นต่ำกว่าหรือสูงกว่า 2.0.15 คับ
วิธีใช้ Unittest Symfony2 บน Netbean
โดย offing อ 30 พ.ย. 2014 3:30 am บอร์ด Software testing
1
1648
อ 30 พ.ย. 2014 9:58 am โดย thatsawan View Topic วิธีใช้ Unittest Symfony2 บน Netbean
public,private และ protect ต่างกันยังไง
โดย offing ส 29 พ.ย. 2014 2:25 pm บอร์ด PHP Knowledge
1
1243
จ 01 ธ.ค. 2014 2:38 pm โดย jataz2 View Topic public,private และ protect ต่างกันยังไง
การทำ Unit Testing บน Joomla 3.3 ด้วย PHPUnit กับ Netbeans 8.0
โดย tsukasaz ศ 28 พ.ย. 2014 4:33 pm บอร์ด Software testing
0
4733
ศ 28 พ.ย. 2014 4:33 pm โดย tsukasaz View Topic การทำ Unit Testing บน Joomla 3.3 ด้วย PHPUnit กับ Netbeans 8.0
สอบถามวิธีการใช้ Jquery picturecrop
โดย offing ศ 28 พ.ย. 2014 4:16 pm บอร์ด Programming - PHP
0
882
ศ 28 พ.ย. 2014 4:16 pm โดย offing View Topic สอบถามวิธีการใช้ Jquery picturecrop
การทำ Unit Testing ด้วย PHPUnit กับ Netbeans 8.0
โดย tsukasaz ศ 28 พ.ย. 2014 3:48 pm บอร์ด Software testing
0
2036
ศ 28 พ.ย. 2014 3:48 pm โดย tsukasaz View Topic การทำ Unit Testing ด้วย PHPUnit กับ Netbeans 8.0
ต้องทำ event ในไฟล์ bbcode
โดย offing ศ 28 พ.ย. 2014 1:39 pm บอร์ด Programming - PHP
1
625
ศ 28 พ.ย. 2014 1:48 pm โดย mindphp View Topic ต้องทำ event ในไฟล์ bbcode