- 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 - FROM 子句
在本章中,我們將介紹 FROM 子句,它與常規 SQL 中的標準 FROM 子句的工作方式完全不同。
查詢始終在特定集合的上下文中執行,並且不能跨集合中的文件進行聯接,這讓我們想知道為什麼我們需要 FROM 子句。事實上,我們不需要它,但如果我們不包含它,那麼我們將無法查詢集合中的文件。
此子句的目的是指定查詢必須在其上操作的資料來源。通常整個集合是源,但可以指定集合的子集。除非源在查詢的後面進行過濾或投影,否則 FROM <from_specification> 子句是可選的。
讓我們再看一遍同一個例子。以下是 **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
}
在上面的查詢中,“**SELECT * FROM c**” 表示整個 Families 集合是需要列舉的源。
子文件
源也可以縮減到較小的子集。當我們只想檢索每個文件中的一個子樹時,子根可以成為源,如下面的示例所示。
當我們執行以下查詢時 -
SELECT * FROM Families.parents
將檢索以下子文件。
[
[
{
"familyName": "Wakefield",
"givenName": "Robin"
},
{
"familyName": "Miller",
"givenName": "Ben"
}
],
[
{
"familyName": "Smith",
"givenName": "James"
},
{
"familyName": "Curtis",
"givenName": "Helen"
}
],
[
{
"firstName": "Thomas",
"relationship": "father"
},
{
"firstName": "Mary Kay",
"relationship": "mother"
}
]
]
透過此查詢的結果,我們可以看到只有父母子文件被檢索。
廣告