---
title: Table.Join – รวมตารางแบบ SQL Join
url: https://www.thepexcel.com/functions/power-query/table-functions/table-join/
type: function-explainer
program: Power Query
syntax: "Table.Join(table1 as table, key1 as any, table2 as table, key2 as any, optional joinKind as nullable number, optional joinAlgorithm as nullable number, optional keyEqualityComparers as nullable list) as table"
date: 2025-12-03
updated: 2025-12-20
scores:
  popularity: 7
  difficulty: 5
  usefulness: 8
---

# Table.Join – รวมตารางแบบ SQL Join

> Table.Join ใช้รวมตารางสองตารางโดยจับคู่ค่าในคอลัมน์กำหนด สนับสนุนทุกประเภท join (Inner, Left Outer,

## คำอธิบาย

Table.Join ใช้รวมตารางสองตารางโดยจับคู่ค่าในคอลัมน์กำหนด สนับสนุนทุกประเภท join (Inner, Left Outer, Right Outer, Full Outer, Anti, Semi) เหมือน SQL Join

## Syntax

```excel
Table.Join(table1 as table, key1 as any, table2 as table, key2 as any, optional joinKind as nullable number, optional joinAlgorithm as nullable number, optional keyEqualityComparers as nullable list) as table
```

## Arguments

| Name | Required | Type | Default | Description |
| --- | --- | --- | --- | --- |
| table1 | Yes | table |  | ตารางแรก (ตารางซ้าย) - ตัวอักษรกำหนดว่าจะรักษาแถวจากตารางนี้ไว้ในจำนวนเท่าใด |
| key1 | Yes | any |  | คอลัมน์ key จากตารางแรก - สามารถระบุเป็น text เดี่ยว "ID" หรือ list หลายคอลัมน์ {"TenantID", "CustomerID"} |
| table2 | Yes | table |  | ตารางที่สอง (ตารางขวา) - ตารางที่ต้องการรวมข้อมูลเข้า |
| key2 | Yes | any |  | คอลัมน์ key จากตารางที่สอง - ต้องตรงกันกับ key1 เพื่อจับคู่ข้อมูล |
| joinKind | No | number |  | ประเภท join (JoinKind.Inner, JoinKind.LeftOuter, JoinKind.RightOuter, JoinKind.FullOuter, JoinKind.LeftAnti, JoinKind.RightAnti, JoinKind.LeftSemi, JoinKind.RightSemi) - ถ้าไม่ระบุจะเป็น JoinKind.Inner |
| joinAlgorithm | No | number |  | อัลกอริทึมที่ใช้ในการ join (ใช้ต่อเมื่อต้องการปรับแต่ง performance - ใช้ไม่บ่อยนัก) |
| keyEqualityComparers | No | list |  | รายการ equality comparer สำหรับการเปรียบเทียบค่า (ใช้ต่อเมื่อต้องการ case-insensitive หรือการเปรียบเทียบพิเศษ) |

## ตัวอย่าง

### 1. Inner Join - จับคู่ลูกค้ากับออเดอร์

```excel
= Table.Join(
    Customers,
    "CustomerID",
    Orders,
    "CustomerID",
    JoinKind.Inner
)
```

**ผลลัพธ์:** `ตารางที่รวมข้อมูลลูกค้า (Name, Phone) กับออเดอร์ (OrderID, Item, Price) - เฉพาะลูกค้าที่มีออเดอร์เท่านั้น`

รวมตารางสองตารางโดยจับคู่คอลัมน์ CustomerID ทั้งสองตาราง - ได้เฉพาะแถวที่มีค่า CustomerID ตรงกันในทั้งสองตาราง

### 2. Left Outer Join - เก็บลูกค้าทั้งหมด

```excel
= let
    Customers = Table.FromRecords({
        [CustomerID = 1, Name = "Bob"],
        [CustomerID = 2, Name = "Alice"],
        [CustomerID = 3, Name = "Charlie"]
    }),
    Orders = Table.FromRecords({
        [CustomerID = 1, Item = "Fishing rod"],
        [CustomerID = 1, Item = "Worms"]
    }),
    Result = Table.Join(
        Customers,
        "CustomerID",
        Orders,
        "CustomerID",
        JoinKind.LeftOuter
    )
in
    Result
```

**ผลลัพธ์:** `4 แถว: Bob (2 ออเดอร์), Alice (null), Charlie (null) - ลูกค้าทั้งหมดมีอยู่แม้ว่าจะไม่มีออเดอร์`

Left Outer Join เก็บแถวทั้งหมดจากตารางซ้าย (Customers) และเพิ่มข้อมูลจากตารางขวา (Orders) ถ้ามี - ถ้าไม่มีการจับคู่ให้ null

### 3. Join หลายคอลัมน์ - จับคู่ TenantID และ CustomerID

```excel
= let
    Customers = Table.FromRecords({
        [TenantID = 1, CustomerID = 1, Name = "Bob"],
        [TenantID = 2, CustomerID = 1, Name = "Alice"]
    }),
    Orders = Table.FromRecords({
        [TenantID = 1, CustomerID = 1, OrderID = 101],
        [TenantID = 2, CustomerID = 1, OrderID = 102]
    }),
    Result = Table.Join(
        Customers,
        {"TenantID", "CustomerID"},
        Orders,
        {"TenantID", "CustomerID"},
        JoinKind.Inner
    )
in
    Result
```

**ผลลัพธ์:** `2 แถว: TenantID=1 (Bob กับ OrderID=101) และ TenantID=2 (Alice กับ OrderID=102) - จับคู่แบบถูกต้องตาม tenant`

ใช้ list key {"TenantID", "CustomerID"} เพื่อจับคู่หลายคอลัมน์พร้อมกัน - ทั้งสองคอลัมน์ต้องตรงกันจึงจะจับคู่ได้

### 4. Anti Join - หาลูกค้าที่ไม่เคยสั่งซื้อ

```excel
= Table.Join(
    Customers,
    "CustomerID",
    Orders,
    "CustomerID",
    JoinKind.LeftAnti
)
```

**ผลลัพธ์:** `แสดงเฉพาะลูกค้าที่ไม่มีแถวตรงกันใน Orders (เช่น Alice และ Charlie ที่ไม่มีออเดอร์)`

Left Anti Join คืนแถวจากตารางซ้ายที่ไม่มีการจับคู่ในตารางขวา - มีประโยชน์สำหรับการหา records ที่ขาดหายไป

## หมายเหตุเพิ่มเติม

- Inner Join = intersect (เฉพาะตรงกัน) | Left Outer = union แต่เก็บซ้ายหมด | Full Outer = union ทั้งหมด - เลือกให้เหมาะสับการใช้งาน

- ถ้าต้องการดูชื่อคอลัมน์ก่อน join ลอง Table.ColumnNames(table1) เพื่อเช็คชื่อ

- Anti Join (LeftAnti/RightAnti) มีประโยชน์ในการหา records ที่ขาดหายไป เช่นลูกค้าที่ไม่เคยสั่งซื้อ

- ถ้า join ช้า อาจเป็นเพราะ key มีประเภทข้อมูลต่างกัน (Text vs Number) - ต้อง convert ให้เหมือนกันก่อน

- Left Outer Join กับ LeftSemi Join ต่างกัน: LeftOuter = เก็บคอลัมน์ทั้งหมด | LeftSemi = เก็บเฉพาะคอลัมน์จากตารางซ้ายเท่านั้น

## คำถามที่พบบ่อย

**Q: ต้องระบุ joinKind ไหม ถ้าไม่ระบุจะเกิดอะไรขึ้น?**

ไม่จำเป็น JoinKind.Inner จะถูกใช้เป็น default ถ้าคุณไม่ระบุ - แต่ถ้าต้อง Left Outer หรือประเภทอื่นต้องระบุชัดเจน

**Q: ถ้า key ไม่ตรงกันหรือค่า null จะเกิดอะไรขึ้น?**

Key ไม่ตรงกัน = ไม่มีการจับคู่ (หรือได้ null ใน Left/Right Outer Join) - ค่า null ใน key ถือว่าไม่ตรงกัน (null ≠ null)

**Q: ต่างกันอย่างไรระหว่าง Inner Join กับ Left Outer Join?**

Inner = เฉพาะแถวที่มีการจับคู่ | Left Outer = เก็บแถวทั้งหมดจากตารางซ้าย + เพิ่มข้อมูลจากตารางขวา (null ถ้าไม่มีการจับคู่)

**Q: สามารถ join มากกว่า 2 คอลัมน์พร้อมกันได้ไหม?**

ได้ ใช้ list key เช่น {"Col1", "Col2", "Col3"} และต้องตรงกันทั้ง 3 คอลัมน์จึงจะจับคู่

**Q: ถ้าตารางมีชื่อคอลัมน์ซ้ำกันจะเกิดอะไรขึ้น?**

Power Query จะเพิ่มเลขตัวเลขให้ (เช่น Name, Name2) - ถ้าไม่อยากให้เกิดปัญหาควร rename หรือ select เฉพาะคอลัมน์ที่ต้องการก่อน join

## ฟังก์ชันที่เกี่ยวข้อง

- [Table.NestedJoin – รวมตารางพร้อมสร้างคอลัมน์ตารางย่อย](https://www.thepexcel.com/functions/power-query/table-functions/table-nestedjoin/)
- [Table.FuzzyJoin – เชื่อมตารางโดยใช้การเปรียบเทียบแบบ Fuzzy (คล้ายกัน)](https://www.thepexcel.com/functions/power-query/table-functions/table-fuzzyjoin/)
- [Table.Combine – รวมหลาย table เข้าด้วยกัน](https://www.thepexcel.com/functions/power-query/table-functions/table-combine/)
- [Table.SelectRows – กรองแถวตามเงื่อนไขใน Power Query](https://www.thepexcel.com/functions/power-query/table-functions/table-selectrows/)
- table-mergecolumns

## แหล่งข้อมูลเพิ่มเติม

- [Microsoft Learn: Table.Join](https://learn.microsoft.com/en-us/powerquery-m/table-join) _(official)_
- [PowerQuery.how - Table.Join Guide](https://powerquery.how/table-join/) _(article)_
- [Power Query JOIN Types Tutorial](https://learn.microsoft.com/en-us/power-bi/fundamentals/power-query-quickstart-using-power-bi) _(official)_

---

_Source: [https://www.thepexcel.com/functions/power-query/table-functions/table-join/](https://www.thepexcel.com/functions/power-query/table-functions/table-join/)_
