การ JOIN เพิ่มเติม

Oracle SQL เบื้องต้น

Sara Billen

Curriculum Manager

ประเภท JOIN อื่น ๆ

  • CROSS JOIN
  • Self JOIN
Oracle SQL เบื้องต้น

Cartesian product (หรือเรียกว่า cross product)

ไดอะแกรม Cartesian product

Oracle SQL เบื้องต้น

Cross product บนตาราง

$$

Cross product บนตาราง

Oracle SQL เบื้องต้น

ตัวอย่าง cross product

SELECT * 
FROM MediaType
| MediaTypeId | Name                        |
|-------------|-----------------------------|
| 1           | MPEG audio file             |
| 2           | Protected AAC audio file    |
| 3           | Protected MPEG-4 video file |
| 4           | Purchased AAC audio file    |
| 5           | AAC audio file              |
Oracle SQL เบื้องต้น

ตัวอย่าง cross product

SELECT m.MediaTypeId, m.Name, t.TrackId, t.Name, t.MediaTypeId
FROM MediaType m CROSS JOIN Track t
| MediaTypeId | Name                        | TrackId | Name       | MediaTypeId       |
|-------------|-----------------------------|---------|------------|-------------------|
| 1           | MPEG audio file             | 3021    | Forty      | 1                 |
| 2           | Protected AAC audio file    | 3021    | Forty      | 1                 |
| 3           | Protected MPEG-4 video file | 3021    | Forty      | 1                 |
| 4           | Purchased AAC audio file    | 3021    | Forty      | 1                 |
| 5           | AAC audio file              | 3021    | Forty      | 1                 |

Oracle SQL เบื้องต้น

Self join

  • JOIN ตารางกับตัวเอง

กรณีการใช้งาน:

SELECT * FROM Employee
| EmployeeId | LastName | Title               | ... | ReportsTo |
|------------|----------|---------------------|-----|-----------|
| 1          | Adames   | General Manager     | ... | null      |
| 2          | Edwards  | Sales Manager       | ... | 1         |
| 3          | Peacock  | Sales Support Agent | ... | 2         |
| 4          | Park     | Sales Support Agent | ... | 2         |

Oracle SQL เบื้องต้น

Self join

SELECT e.LastName Employee, m.LastName ReportsTo
FROM Employee e JOIN Employee m
ON (e.ReportsTo = m.EmployeeId)
| Employee | ReportsTo |
|----------|-----------|
| Edwards  | Adams     |
| Peacock  | Edwards   |
| Mitchell | Adams     |
| Park       | Edwards   |
| ...      | ...       |
Oracle SQL เบื้องต้น

มาฝึกกันเถอะ!

Oracle SQL เบื้องต้น

Preparing Video For Download...