- DocumentDB SQL 教程
- DocumentDB SQL - 首頁
- DocumentDB SQL - 概述
- DocumentDB SQL - Select 子句
- DocumentDB SQL - From 子句
- DocumentDB SQL - Where 子句
- DocumentDB SQL - 運算子
- DocumentDB - Between 關鍵字
- DocumentDB SQL - In 關鍵字
- DocumentDB SQL - Value 關鍵字
- DocumentDB SQL - Order By 子句
- DocumentDB SQL - 迭代
- DocumentDB SQL - 聯接
- DocumentDB SQL - 別名
- DocumentDB SQL - 陣列建立
- DocumentDB - 標量表達式
- DocumentDB SQL - 引數化
- DocumentDB SQL - 內建函式
- Linq to SQL 轉換
- JavaScript 整合
- 使用者定義函式
- 複合 SQL 查詢
- DocumentDB SQL 有用資源
- DocumentDB SQL - 快速指南
- DocumentDB SQL - 有用資源
- DocumentDB SQL - 討論
DocumentDB SQL - 聯接
在關係資料庫中,Joins 子句用於組合來自資料庫中兩個或多個表的記錄,並且在設計規範化模式時,跨表聯接的需求非常重要。由於 DocumentDB 處理無模式文件的非規範化資料模型,因此 DocumentDB SQL 中的 JOIN 等同於“自聯接”。
讓我們考慮前面示例中的三個文件。
以下是AndersenFamily 文件。
{
"id": "AndersenFamily",
"lastName": "Andersen",
"parents": [
{ "firstName": "Thomas", "relationship": "father" },
{ "firstName": "Mary Kay", "relationship": "mother" }
],
"children": [
{
"firstName": "Henriette Thaulow",
"gender": "female",
"grade": 5,
"pets": [ { "givenName": "Fluffy", "type": "Rabbit" } ]
}
],
"location": { "state": "WA", "county": "King", "city": "Seattle" },
"isRegistered": true
}
以下是SmithFamily 文件。
{
"id": "SmithFamily",
"parents": [
{ "familyName": "Smith", "givenName": "James" },
{ "familyName": "Curtis", "givenName": "Helen" }
],
"children": [
{
"givenName": "Michelle",
"gender": "female",
"grade": 1
},
{
"givenName": "John",
"gender": "male",
"grade": 7,
"pets": [
{ "givenName": "Tweetie", "type": "Bird" }
]
}
],
"location": {
"state": "NY",
"county": "Queens",
"city": "Forest Hills"
},
"isRegistered": true
}
以下是WakefieldFamily 文件。
{
"id": "WakefieldFamily",
"parents": [
{ "familyName": "Wakefield", "givenName": "Robin" },
{ "familyName": "Miller", "givenName": "Ben" }
],
"children": [
{
"familyName": "Merriam",
"givenName": "Jesse",
"gender": "female",
"grade": 6,
"pets": [
{ "givenName": "Charlie Brown", "type": "Dog" },
{ "givenName": "Tiger", "type": "Cat" },
{ "givenName": "Princess", "type": "Cat" }
]
},
{
"familyName": "Miller",
"givenName": "Lisa",
"gender": "female",
"grade": 3,
"pets": [
{ "givenName": "Jake", "type": "Snake" }
]
}
],
"location": { "state": "NY", "county": "Manhattan", "city": "NY" },
"isRegistered": false
}
讓我們看一個例子來了解 JOIN 子句是如何工作的。
以下是將根與子文件 children 聯接的查詢。
SELECT f.id FROM Families f JOIN c IN f.children
執行上述查詢後,將產生以下輸出。
[
{
"id": "WakefieldFamily"
},
{
"id": "WakefieldFamily"
},
{
"id": "SmithFamily"
},
{
"id": "SmithFamily"
},
{
"id": "AndersenFamily"
}
]
在上面的示例中,聯接是在文件根和子根 children 之間進行的,它在兩個 JSON 物件之間進行笛卡爾積。以下是一些需要注意的事項:
在 FROM 子句中,JOIN 子句是一個迭代器。
前兩個文件 WakefieldFamily 和 SmithFamily 包含兩個子項,因此結果集也包含笛卡爾積,它為每個子項生成一個單獨的物件。
第三個文件 AndersenFamily 只包含一個子項,因此此文件只有一個對應物件。
讓我們再看一個相同的例子,但是這次我們也檢索子項名稱,以便更好地理解 JOIN 子句。
以下是將根與子文件 children 聯接的查詢。
SELECT f.id AS familyName, c.givenName AS childGivenName, c.firstName AS childFirstName FROM Families f JOIN c IN f.children
執行上述查詢後,將產生以下輸出。
[
{
"familyName": "WakefieldFamily",
"childGivenName": "Jesse"
},
{
"familyName": "WakefieldFamily",
"childGivenName": "Lisa"
},
{
"familyName": "SmithFamily",
"childGivenName": "Michelle"
},
{
"familyName": "SmithFamily",
"childGivenName": "John"
},
{
"familyName": "AndersenFamily",
"childFirstName": "Henriette Thaulow"
}
]
廣告