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 
} 
WakefieldFamily

在上面的查詢中,“**SELECT * FROM c**” 表示整個 Families 集合是需要列舉的源。

子文件

源也可以縮減到較小的子集。當我們只想檢索每個文件中的一個子樹時,子根可以成為源,如下面的示例所示。

Sub-documents

當我們執行以下查詢時 -

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" 
      } 
   ] 
]

透過此查詢的結果,我們可以看到只有父母子文件被檢索。

廣告

© . All rights reserved.