

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 运算符
<a name="iot-sql-operators"></a>

可以在 SELECT 和 WHERE 子句中使用以下运算符。

## AND 运算符
<a name="iot-sql-operators-and"></a>

返回 `Boolean` 结果。执行逻辑与运算。如果左右操作数为 true，则返回 true。否则，返回 false。需要提供 `Boolean` 操作数或不区分大小写的“true”或“false”字符串操作数。

*语法：*` expression AND expression`。


**AND 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Boolean | Boolean | Boolean。如果两个操作数均为 true，则为 true。否则为 false。 | 
| String/Boolean | String/Boolean | 如果所有字符串均为“true”或“false”（不区分大小写），则它们将被转换为 Boolean 并作为 boolean AND boolean 正常处理。 | 
| 其它值 | 其它值 | Undefined. | 

## OR 运算符
<a name="iot-sql-operators-or"></a>

返回 `Boolean` 结果。执行逻辑或运算。如果左右操作数至少有一个为 true，则返回 true。否则，返回 false。需要提供 `Boolean` 操作数或不区分大小写的“true”或“false”字符串操作数。

*语法：*` expression OR expression`。


**OR 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Boolean | Boolean | Boolean。如果任意一个操作数为 true，则为 true。否则为 false。 | 
| String/Boolean | String/Boolean | 如果所有字符串均为“true”或“false”（不区分大小写），则将其转换为布尔值并作为 boolean OR boolean 正常处理。 | 
| 其它值 | 其它值 | Undefined. | 

## NOT 运算符
<a name="iot-sql-operators-not"></a>

返回 `Boolean` 结果。执行逻辑非运算。如果操作数为 false，则返回 true。否则返回 true。需要 `Boolean` 操作数或不区分大小写的“true”或“false”字符串操作数。

*语法：*`NOT expression`。


**NOT 运算符**  

| 操作数 | Output | 
| --- | --- | 
| Boolean | Boolean。如果操作数为 false，则为 true。否则为 true。 | 
| String | 如果字符串为“true”或“false”（不区分大小写），它将转换为对应的布尔值，并返回相反的值。 | 
| 其它值 | Undefined. | 

## IN 运算符
<a name="iot-sql-operators-in"></a>

返回 `Boolean` 结果。您可以在 WHERE 子句中使用 IN 运算符来查看某个值是否与数组中的任何值匹配。如果找到匹配项，则返回 true，否则返回 false。

*语法：*` expression IN expression`。


**IN 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Int/Decimal/String/Array/Object | Array | 如果在数组中找到 Integer/Decimal/String/Array/Object 元素，则为 true。否则为 false。 | 

*示例*：

```
SQL: "select * from 'a/b' where 3 in arr"

JSON: {"arr":[1, 2, 3, "three", 5.7, null]}
```

在此示例中，条件子句 `where 3 in arr` 的计算结果将为 true，因为名为 `arr` 的数组中存在 3 。因此，在 SQL 语句中，`select * from 'a/b'` 将执行。此示例还显示了数组可以是异构的。

## EXISTS 运算符
<a name="iot-sql-operators-exists"></a>

返回 `Boolean` 结果。可以在条件子句中使用 EXISTS 运算符来测试子查询中是否存在元素。如果子查询返回一个或多个元素，则返回 true；如果子查询不返回任何元素，则返回 false。

*语法：*` expression`。

*示例*：

```
SQL: "select * from 'a/b' where exists (select * from arr as a where a = 3)"

JSON: {"arr":[1, 2, 3]}
```

在此示例中，条件子句 `where exists (select * from arr as a where a = 3)` 的计算结果将为 true，因为名为 `arr` 的数组中存在 3 。因此，在 SQL 语句中，`select * from 'a/b'` 将执行。

*示例*：

```
SQL: select * from 'a/b' where exists (select * from e as e where foo = 2)

JSON: {"foo":4,"bar":5,"e":[{"foo":1},{"foo":2}]}
```

在此示例中，条件子句 `where exists (select * from e as e where foo = 2)` 的计算结果将为 true，因为 JSON 对象中的 `e` 数组包含对象 `{"foo":2}`。因此，在 SQL 语句中，`select * from 'a/b'` 将执行。

## > 运算符
<a name="iot-sql-operators-greater"></a>

返回 `Boolean` 结果。如果左侧操作数大于右侧操作数，则返回 true。两个操作数将转换为 `Decimal`，然后进行比较。

*语法：* `expression > expression`。


**> 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Int/Decimal | Int/Decimal | Boolean。如果左侧操作数大于右侧操作数，则为 true。否则为 false。 | 
| String/Int/Decimal | String/Int/Decimal | 如果所有字符串可以转换为 Decimal，则为 Boolean。如果左侧操作数大于右侧操作数，则返回 true。否则为 false。 | 
| 其它值 | Undefined. | Undefined. | 

## >= operator
<a name="iot-sql-operators-greater-equal"></a>

返回 `Boolean` 结果。如果左侧操作数大于等于右侧操作数，则返回 true。两个操作数将转换为 `Decimal`，然后进行比较。

*语法：*`expression >= expression`。


**>= operator**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Int/Decimal | Int/Decimal | Boolean。如果左侧操作数大于等于右侧操作数，则为 true。否则为 false。 | 
| String/Int/Decimal | String/Int/Decimal | 如果所有字符串可以转换为 Decimal，则为 Boolean。如果左侧操作数大于等于右侧操作数，则返回 true。否则为 false。 | 
| 其它值 | Undefined. | Undefined. | 

## < 运算符
<a name="iot-sql-operators-less"></a>

返回 `Boolean` 结果。如果左侧操作数小于右侧操作数，则返回 true。两个操作数将转换为 `Decimal`，然后进行比较。

*语法：*`expression < expression`。


**< 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Int/Decimal | Int/Decimal | Boolean。如果左侧操作数小于右侧操作数，则为 true。否则为 false。 | 
| String/Int/Decimal | String/Int/Decimal | 如果所有字符串可以转换为 Decimal，则为 Boolean。如果左侧操作数小于右侧操作数，则返回 true。否则为 false。 | 
| 其它值 | Undefined | Undefined | 

## <= 运算符
<a name="iot-sql-operators-less-equal"></a>

返回 `Boolean` 结果。如果左侧操作数小于等于右侧操作数，则返回 true。两个操作数将转换为 `Decimal`，然后进行比较。

*语法：*`expression <= expression`。


**<= 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Int/Decimal | Int/Decimal | Boolean。如果左侧操作数小于等于右侧操作数，则为 true。否则为 false。 | 
| String/Int/Decimal | String/Int/Decimal | 如果所有字符串可以转换为 Decimal，则为 Boolean。如果左侧操作数小于等于右侧操作数，则返回 true。否则为 false。 | 
| 其它值 | Undefined | Undefined | 

## <> 运算符
<a name="iot-sql-operators-not-eq"></a>

返回 `Boolean` 结果。如果左右操作数不相等，则返回 true。否则返回 false。

*语法：*` expression <> expression`。


**<> 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Int | Int | 如果左侧操作数不等于右侧操作数，则为 true。否则为 false。 | 
| Decimal | Decimal | 如果左侧操作数不等于右侧操作数，则为 true。否则为 false。在比较之前，Int 会被转换为 Decimal。 | 
| String | String | 如果左侧操作数不等于右侧操作数，则为 true。否则为 false。 | 
| 数组 | 数组 | 如果各个操作数中的项不相等且顺序不同，则为 true。否则为 false | 
| 对象 | 对象 | 如果各个操作数的键和值均不相等，则为 true。否则为 false。的顺序 keys/values 并不重要。 | 
| Null | Null | False。 | 
| 任意值 | Undefined | Undefined。 | 
| Undefined | 任意值 | Undefined。 | 
| 不匹配的类型 | 不匹配的类型 | True。 | 

## = 运算符
<a name="iot-sql-operators-eq"></a>

返回 `Boolean` 结果。如果左右操作数相等，则返回 true。否则返回 false。

*语法：*` expression = expression`。


**= 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Int | Int | 如果左侧操作数等于右侧操作数，则为 true。否则为 false。 | 
| Decimal | Decimal | 如果左侧操作数等于右侧操作数，则为 true。否则为 false。在比较之前，Int 会被转换为 Decimal。 | 
| String | String | 如果左侧操作数等于右侧操作数，则为 true。否则为 false。 | 
| 数组 | 数组 | 如果各个操作数中的项相等且顺序相同，则为 true。否则为 false。 | 
| 对象 | 对象 | 如果各个操作数的键和值相等，则为 true。否则为 false。的顺序 keys/values 并不重要。 | 
| 任意值 | Undefined | Undefined. | 
| Undefined | 任意值 | Undefined. | 
| 不匹配的类型 | 不匹配的类型 | False。 | 

## \$1 运算符
<a name="iot-sql-operators-plus"></a>

“\$1”是一个重载运算符。它可用于字符串联接或相加。

*语法：*` expression + expression`。


**\$1 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| String | 任意值 | 将右侧操作数转换为一个字符串，并联接到左侧操作数的末尾。 | 
| 任意值 | String | 将左侧操作数转换为一个字符串，并将右侧操作数联接到转换后的左侧操作数的末尾。 | 
| Int | Int | Int 值。将操作数相加。 | 
| Int/Decimal | Int/Decimal | Decimal 值。将操作数相加。 | 
| 其它值 | 其它值 | Undefined. | 

## - 运算符
<a name="iot-sql-operators-sub"></a>

从左侧操作数中减去右侧操作数。

*语法：*` expression - expression`。


**- 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Int | Int | Int 值。从左侧操作数中减去右侧操作数。 | 
| Int/Decimal | Int/Decimal | Decimal 值。从左侧操作数中减去右侧操作数。 | 
| String/Int/Decimal | String/Int/Decimal | 如果所有字符串都正确地转换为小数，则返回 Decimal 值。从左侧操作数中减去右侧操作数。否则返回 Undefined。 | 
| 其它值 | 其它值 | Undefined. | 
| 其它值 | 其它值 | Undefined. | 

## \$1 运算符
<a name="iot-sql-operators-mult"></a>

左侧操作数乘以右侧操作数。

*语法：*` expression * expression`。


**\$1 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Int | Int | Int 值。左侧操作数乘以右侧操作数。 | 
| Int/Decimal | Int/Decimal | Decimal 值。左侧操作数乘以右侧操作数。 | 
| String/Int/Decimal | String/Int/Decimal | 如果所有字符串都正确地转换为小数，则返回 Decimal 值。左侧操作数乘以右侧操作数。否则返回 Undefined。 | 
| 其它值 | 其它值 | Undefined. | 

## / 运算符
<a name="iot-sql-operators-div"></a>

左侧操作数除以右侧操作数。

*语法：*` expression / expression`。


**/ 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Int | Int | Int 值。左侧操作数除以右侧操作数。 | 
| Int/Decimal | Int/Decimal | Decimal 值。左侧操作数除以右侧操作数。 | 
| String/Int/Decimal | String/Int/Decimal | 如果所有字符串都正确地转换为小数，则返回 Decimal 值。左侧操作数除以右侧操作数。否则返回 Undefined。 | 
| 其它值 | 其它值 | Undefined. | 

## % 运算符
<a name="iot-sql-operators-mod"></a>

返回左侧操作数除以右侧操作数得到的余数。

*语法：*` expression % expression`。


**% 运算符**  

| 左侧操作数 | 右侧操作数 | Output | 
| --- | --- | --- | 
| Int | Int | Int 值。返回左侧操作数除以右侧操作数得到的余数。 | 
| String/Int/Decimal | String/Int/Decimal | 如果所有字符串都正确地转换为小数，则返回 Decimal 值。返回左侧操作数除以右侧操作数得到的余数。否则为 Undefined。 | 
| 其它值 | 其它值 | Undefined. | 