

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 運算子
<a name="iot-sql-operators"></a>

下列運算子可用於 SELECT 和 WHERE 子句。

## AND 運算子
<a name="iot-sql-operators-and"></a>

傳回 `Boolean` 結果。執行邏輯 AND 運算。如果左右運算元為 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` 結果。執行邏輯 OR 運算。如果右運算元或左運算元有一個為 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` 結果。執行邏輯 NOT 運算。如果運算元為 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，因為 3 存在於名為 的陣列中`arr`。因此，在 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，因為 3 存在於名為 的陣列中`arr`。因此，在 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. | 

## >= 運算子
<a name="iot-sql-operators-greater-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-less"></a>

傳回 `Boolean` 結果。如果左側運算元少於右運算元。兩個運算元均轉換為 `Decimal`，再做比較。

*語法：*`expression < expression`。


**< 運算子**  

| 左運算元 | 右運算元 | Output | 
| --- | --- | --- | 
| Int/Decimal | Int/Decimal | Boolean。如果左運算元小於右運算元即為 true。否則為 false。 | 
| String/Int/Decimal | String/Int/Decimal | 如果所有字串均可轉換為 Decimal，即 Boolean。如果左側運算元少於右運算元。否則為 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。鍵/值的順序不重要。 | 
| Null | Null | False。 | 
| 任何值 | 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。鍵/值的順序不重要。 | 
| 任何值 | 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. | 