📜  DocumentDB SQL-运算符

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


运算符是保留字或字符,主要用于SQL WHERE子句中以执行操作,例如比较和算术运算。 DocumentDB SQL还支持各种标量表达式。最常用的是二进制和一元表达式

当前支持以下SQL运算符,并且可以在查询中使用。

SQL比较运算符

以下是所有DocumentDB SQL语法中可用的比较运算符的列表。

S.No. Operators & Description
1

=

Checks if the values of two operands are equal or not. If yes, then condition becomes true.

2

!=

Checks if the values of two operands are equal or not. If values are not equal then condition becomes true.

3

<>

Checks if the values of two operands are equal or not. If values are not equal then condition becomes true.

4

>

Checks if the value of left operand is greater than the value of right operand. If yes, then condition becomes true.

5

<

Checks if the value of left operand is less than the value of right operand. If yes, then condition becomes true.

6

>=

Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then condition becomes true.

7

<=

Checks if the value of left operand is less than or equal to the value of right operand. If yes, then condition becomes true.

SQL逻辑运算符

以下是DocumentDB SQL语法中可用的所有逻辑运算符的列表。

S.No. Operators & Description
1

AND

The AND operator allows the existence of multiple conditions in an SQL statement’s WHERE clause.

2

BETWEEN

The BETWEEN operator is used to search for values that are within a set of values, given the minimum value and the maximum value.

3

IN

The IN operator is used to compare a value to a list of literal values that have been specified.

4

OR

The OR operator is used to combine multiple conditions in an SQL statement’s WHERE clause.

5

NOT

The NOT operator reverses the meaning of the logical operator with which it is used. For example, NOT EXISTS, NOT BETWEEN, NOT IN, etc. This is a negate operator.

SQL算术运算符

以下是DocumentDB SQL语法中所有可用的算术运算运算符的列表。

S.No. Operators & Description
1

+

Addition − Adds values on either side of the operator.

2

Subtraction − Subtracts the right hand operand from the left hand operand.

3

*

Multiplication − Multiplies values on either side of the operator.

4

/

Division − Divides the left hand operand by the right hand operand.

5

%

Modulus − Divides the left hand operand by the right hand operand and returns the remainder.

在此示例中,我们还将考虑相同的文档。以下是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 
}

让我们来看看一个简单的例子,其中一个运算符在WHERE子句中使用。

比较运算符

在此查询中,在WHERE子句中,指定了(WHERE f.id =“ WakefieldFamily”)条件,它将检索ID等于WakefieldFamily的文档。

SELECT * 
FROM f  
WHERE f.id = "WakefieldFamily"

执行上述查询后,它将返回WakefieldFamily的完整JSON文档,如以下输出所示。

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

让我们看另一个示例,其中查询将检索成绩大于5的子级数据。

SELECT * 
FROM Families.children[0] c 
WHERE (c.grade > 5)

执行上述查询后,它将检索以下子文档,如输出所示。

[
   {
      "familyName": "Merriam", 
      "givenName": "Jesse", 
      "gender": "female", 
      "grade": 6, 
        
      "pets": [
         { 
            "givenName": "Charlie Brown", 
            "type": "Dog" 
         }, 
            
         { 
            "givenName": "Tiger", 
            "type": "Cat" 
         }, 
            
         { 
            "givenName": "Princess", 
            "type": "Cat" 
         } 
      ] 
   } 
]