Documentdb Sql 简明教程
DocumentDB SQL - Operators
运算符是主要在 SQL WHERE 子句中用来执行运算(如比较和算术运算)的保留字或字符。DocumentDB SQL 还支持各种标量表达式。最常用的表达式是 binary and unary expressions 。
目前支持以下 SQL 运算符,并且可以在查询中使用它们。
SQL Comparison Operators
以下是 DocumentDB SQL 语法中所有比较运算符的列表。
S.No. |
Operators & Description |
1 |
= 检查两个操作数的值是否相等。如果相等,则条件为 true。 |
2 |
!= 检查两个操作数的值是否不等。如果不等,则条件为 true。 |
3 |
<> 检查两个操作数的值是否不等。如果不等,则条件为 true。 |
4 |
> 检查左操作数的值是否大于右操作数的值。如果相等,则条件为 true。 |
5 |
< 检查左操作数的值是否小于右操作数的值。如果相等,则条件为 true。 |
6 |
>= 检查左操作数的值是否大于或等于右操作数的值。如果相等,则条件为 true。 |
7 |
⇐ 检查左操作数的值是否小于或等于右操作数的值。如果相等,则条件为 true。 |
SQL Logical Operators
以下是 DocumentDB SQL 语法中所有逻辑运算符的列表。
S.No. |
Operators & Description |
1 |
AND AND 运算符允许在 SQL 语句的 WHERE 子句中存在多个条件。 |
2 |
BETWEEN BETWEEN 运算符用于搜索给定最小值和最大值的集合值。 |
3 |
IN IN 运算符用于将一个值与已指定的一系列文本值进行比较。 |
4 |
OR OR 运算符用于组合 SQL 语句 WHERE 子句中的多个条件。 |
5 |
NOT NOT 运算符会逆转与之结合使用的逻辑运算符的含义。例如,NOT EXISTS、NOT BETWEEN、NOT IN 等。这是一个否定运算符。 |
SQL Arithmetic Operators
以下是 DocumentDB SQL 语法中所有算术运算符的列表。
S.No. |
Operators & Description |
1 |
+ Addition - 加法:将运算符两侧的值相加。 |
2 |
- Subtraction - 减法:用左操作数减去右操作数。 |
3 |
Multiplication - 乘法:将运算符两侧的值相乘。 |
4 |
/ Division - 除法:用左操作数除以右操作数。 |
5 |
% Modulus − 将左操作数除以右操作数并返回余数。 |
在这个示例中我们也会考虑相同的文档。以下是 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"
}
]
}
]