📜  DocumentDB SQL-关键字

📅  最后修改于: 2020-11-28 13:34:56             🧑  作者: Mango


IN关键字可用于检查指定值是否与列表中的任何值匹配。 IN运算符允许您在WHERE子句中指定多个值。 IN等效于链接多个OR子句。

与前面的示例一样,我们认为这三个文档也是如此。以下是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 
}

让我们看一个简单的例子。

在关键字中

以下是查询,该查询将检索其familyName为“ Smith”或Wakefield的数据。

SELECT * 
FROM Families.parents[0] f 
WHERE f.familyName IN ('Smith', 'Wakefield')

执行上述查询后,将产生以下输出。

[ 
   { 
      "familyName": "Wakefield", 
      "givenName": "Robin" 
   }, 
    
   { 
      "familyName": "Smith", 
      "givenName": "James" 
   } 
]

让我们考虑另一个简单的示例,其中将检索所有家庭文档,其中id为“ SmithFamily”或“ AndersenFamily”之一。以下是查询。

SELECT * 
FROM Families  
WHERE Families.id IN ('SmithFamily', 'AndersenFamily')

执行上述查询后,将产生以下输出。

[ 
   { 
      "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, 
      "_rid": "Ic8LAJFujgEDAAAAAAAAAA==", 
      "_ts": 1450541623, 
      "_self": "dbs/Ic8LAA==/colls/Ic8LAJFujgE=/docs/Ic8LAJFujgEDAAAAAAAAAA==/", 
      "_etag": "\"00000600-0000-0000-0000-567582370000\"", 
      "_attachments": "attachments/" 
   },
    
   { 
      "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, 
      "_rid": "Ic8LAJFujgEEAAAAAAAAAA==", 
      "_ts": 1450541624, 
      "_self": "dbs/Ic8LAA==/colls/Ic8LAJFujgE=/docs/Ic8LAJFujgEEAAAAAAAAAA==/", 
      "_etag": "\"00000700-0000-0000-0000-567582380000\"", 
      "_attachments": "attachments/" 
   }
]