diff --git a/src/UserGuide/Master/Table/SQL-Manual/From-Join-Clause.md b/src/UserGuide/Master/Table/SQL-Manual/From-Join-Clause.md index 06c1835b1..2e686131e 100644 --- a/src/UserGuide/Master/Table/SQL-Manual/From-Join-Clause.md +++ b/src/UserGuide/Master/Table/SQL-Manual/From-Join-Clause.md @@ -70,10 +70,9 @@ The `JOIN` clause combines two tables based on specific conditions, typically pr In the current version of IoTDB, the following joins are supported: -1. **Inner Join**: Combines rows that meet the join condition, effectively returning the intersection of the two tables. -2. **Full Outer Join**: Returns all records from both tables, inserting `NULL` values for unmatched rows. - -**Note:** The join condition in IoTDB must be an equality condition on the `time` column. This restriction ensures that rows are joined based on the equality of their timestamps. +1. **Inner Join**: Combines rows that meet the join condition, effectively returning the intersection of the two tables. The join condition must be an equality condition on the `time` column. +2. **Full Outer Join**: Returns all records from both tables, inserting `NULL` values for unmatched rows. The join condition can be any equality expression. +3. **Cross Join**: Represents the Cartesian product of two tables. ### 3.1 Inner Join @@ -120,6 +119,19 @@ An **outer join** returns rows even when no matching records exist in the other IoTDB currently supports only `FULL [OUTER] JOIN`. This type returns all records from both tables. If a record in one table has no match in the other, `NULL` values are returned for the unmatched fields. `FULL JOIN` **must use explicit join conditions**. +```sql +//Specify the join condition after the ON keyword or specify the join columns after the USING keyword. +SELECT selectExpr [, selectExpr] ... FROM FULL [OUTER] JOIN joinCriteria [WHERE whereCondition] + +joinCriteria + : ON booleanExpression + | USING '(' identifier (',' identifier)* ')' + ; +``` + +### 3.3 Cross Join +A cross join represents the Cartesian product of two tables, returning all possible combinations of the N rows from the left table and the M rows from the right table, resulting in N*M rows. This type of join is the least commonly used in practice. + ## 4. Example Queries The [Example Data page](../Reference/Sample-Data.md)page provides SQL statements to construct table schemas and insert data. By downloading and executing these statements in the IoTDB CLI, you can import the data into IoTDB. This data can be used to test and run the example SQL queries included in this documentation, allowing you to reproduce the described results. @@ -399,4 +411,92 @@ Query Results: +-----------------------------+-------+------------+-------+------------+ Total line number = 21 It costs 0.073s +``` + +Example 3: The join condition is based on a non-time column. + +```sql +SELECT + region, + t1.time as time1, + t1.temperature as temperature1, + t2.time as time2, + t2.temperature as temperature2 +FROM + table1 t1 FULL JOIN table2 t2 +USING(region) +LIMIT 10 +``` + +Query Results: + +```sql ++------+-----------------------------+------------+-----------------------------+------------+ +|region| time1|temperature1| time2|temperature2| ++------+-----------------------------+------------+-----------------------------+------------+ +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-29T11:00:00.000+08:00| null| +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-28T08:00:00.000+08:00| 85.0| +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-30T00:00:00.000+08:00| 90.0| +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-29T00:00:00.000+08:00| 85.0| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-29T11:00:00.000+08:00| null| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-28T08:00:00.000+08:00| 85.0| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-30T00:00:00.000+08:00| 90.0| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-29T00:00:00.000+08:00| 85.0| +| 上海|2024-11-29T18:30:00.000+08:00| 90.0|2024-11-29T11:00:00.000+08:00| null| +| 上海|2024-11-29T18:30:00.000+08:00| 90.0|2024-11-28T08:00:00.000+08:00| 85.0| ++------+-----------------------------+------------+-----------------------------+------------+ +Total line number = 10 +It costs 0.040s +``` + +#### 4.2.3 Cross Join + +**Example 1: Explicit Join** + +```sql +SELECT table1.*, table2.* FROM table1 CROSS JOIN table2 LIMIT 8; +``` + +Query Results: + +```sql ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-27T00:00:00.000+08:00| 北京| 1001| 101| B| 180| 85.0| 35.1| true|2024-11-27T16:37:01.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-28T08:00:00.000+08:00| 上海| 3001| 100| C| 90| 85.0| 35.2| false|2024-11-28T08:00:09.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-26T13:37:00.000+08:00| 北京| 1001| 100| A| 180| 90.0| 35.1| true|2024-11-26T13:37:34.000+08:00| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +Total line number = 8 +It costs 0.282s +``` + +**Example 2: Implicit Join** + +```sql +SELECT table1.*, table2.* FROM table1, table2 LIMIT 8; +``` + +Query Results: + +```sql ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-27T00:00:00.000+08:00| 北京| 1001| 101| B| 180| 85.0| 35.1| true|2024-11-27T16:37:01.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-28T08:00:00.000+08:00| 上海| 3001| 100| C| 90| 85.0| 35.2| false|2024-11-28T08:00:09.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-26T13:37:00.000+08:00| 北京| 1001| 100| A| 180| 90.0| 35.1| true|2024-11-26T13:37:34.000+08:00| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +Total line number = 8 +It costs 0.047s ``` \ No newline at end of file diff --git a/src/UserGuide/latest-Table/SQL-Manual/From-Join-Clause.md b/src/UserGuide/latest-Table/SQL-Manual/From-Join-Clause.md index 06c1835b1..2e686131e 100644 --- a/src/UserGuide/latest-Table/SQL-Manual/From-Join-Clause.md +++ b/src/UserGuide/latest-Table/SQL-Manual/From-Join-Clause.md @@ -70,10 +70,9 @@ The `JOIN` clause combines two tables based on specific conditions, typically pr In the current version of IoTDB, the following joins are supported: -1. **Inner Join**: Combines rows that meet the join condition, effectively returning the intersection of the two tables. -2. **Full Outer Join**: Returns all records from both tables, inserting `NULL` values for unmatched rows. - -**Note:** The join condition in IoTDB must be an equality condition on the `time` column. This restriction ensures that rows are joined based on the equality of their timestamps. +1. **Inner Join**: Combines rows that meet the join condition, effectively returning the intersection of the two tables. The join condition must be an equality condition on the `time` column. +2. **Full Outer Join**: Returns all records from both tables, inserting `NULL` values for unmatched rows. The join condition can be any equality expression. +3. **Cross Join**: Represents the Cartesian product of two tables. ### 3.1 Inner Join @@ -120,6 +119,19 @@ An **outer join** returns rows even when no matching records exist in the other IoTDB currently supports only `FULL [OUTER] JOIN`. This type returns all records from both tables. If a record in one table has no match in the other, `NULL` values are returned for the unmatched fields. `FULL JOIN` **must use explicit join conditions**. +```sql +//Specify the join condition after the ON keyword or specify the join columns after the USING keyword. +SELECT selectExpr [, selectExpr] ... FROM FULL [OUTER] JOIN joinCriteria [WHERE whereCondition] + +joinCriteria + : ON booleanExpression + | USING '(' identifier (',' identifier)* ')' + ; +``` + +### 3.3 Cross Join +A cross join represents the Cartesian product of two tables, returning all possible combinations of the N rows from the left table and the M rows from the right table, resulting in N*M rows. This type of join is the least commonly used in practice. + ## 4. Example Queries The [Example Data page](../Reference/Sample-Data.md)page provides SQL statements to construct table schemas and insert data. By downloading and executing these statements in the IoTDB CLI, you can import the data into IoTDB. This data can be used to test and run the example SQL queries included in this documentation, allowing you to reproduce the described results. @@ -399,4 +411,92 @@ Query Results: +-----------------------------+-------+------------+-------+------------+ Total line number = 21 It costs 0.073s +``` + +Example 3: The join condition is based on a non-time column. + +```sql +SELECT + region, + t1.time as time1, + t1.temperature as temperature1, + t2.time as time2, + t2.temperature as temperature2 +FROM + table1 t1 FULL JOIN table2 t2 +USING(region) +LIMIT 10 +``` + +Query Results: + +```sql ++------+-----------------------------+------------+-----------------------------+------------+ +|region| time1|temperature1| time2|temperature2| ++------+-----------------------------+------------+-----------------------------+------------+ +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-29T11:00:00.000+08:00| null| +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-28T08:00:00.000+08:00| 85.0| +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-30T00:00:00.000+08:00| 90.0| +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-29T00:00:00.000+08:00| 85.0| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-29T11:00:00.000+08:00| null| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-28T08:00:00.000+08:00| 85.0| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-30T00:00:00.000+08:00| 90.0| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-29T00:00:00.000+08:00| 85.0| +| 上海|2024-11-29T18:30:00.000+08:00| 90.0|2024-11-29T11:00:00.000+08:00| null| +| 上海|2024-11-29T18:30:00.000+08:00| 90.0|2024-11-28T08:00:00.000+08:00| 85.0| ++------+-----------------------------+------------+-----------------------------+------------+ +Total line number = 10 +It costs 0.040s +``` + +#### 4.2.3 Cross Join + +**Example 1: Explicit Join** + +```sql +SELECT table1.*, table2.* FROM table1 CROSS JOIN table2 LIMIT 8; +``` + +Query Results: + +```sql ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-27T00:00:00.000+08:00| 北京| 1001| 101| B| 180| 85.0| 35.1| true|2024-11-27T16:37:01.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-28T08:00:00.000+08:00| 上海| 3001| 100| C| 90| 85.0| 35.2| false|2024-11-28T08:00:09.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-26T13:37:00.000+08:00| 北京| 1001| 100| A| 180| 90.0| 35.1| true|2024-11-26T13:37:34.000+08:00| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +Total line number = 8 +It costs 0.282s +``` + +**Example 2: Implicit Join** + +```sql +SELECT table1.*, table2.* FROM table1, table2 LIMIT 8; +``` + +Query Results: + +```sql ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-27T00:00:00.000+08:00| 北京| 1001| 101| B| 180| 85.0| 35.1| true|2024-11-27T16:37:01.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-28T08:00:00.000+08:00| 上海| 3001| 100| C| 90| 85.0| 35.2| false|2024-11-28T08:00:09.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-26T13:37:00.000+08:00| 北京| 1001| 100| A| 180| 90.0| 35.1| true|2024-11-26T13:37:34.000+08:00| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +Total line number = 8 +It costs 0.047s ``` \ No newline at end of file diff --git a/src/zh/UserGuide/Master/Table/SQL-Manual/From-Join-Clause.md b/src/zh/UserGuide/Master/Table/SQL-Manual/From-Join-Clause.md index b37633729..8a94550ef 100644 --- a/src/zh/UserGuide/Master/Table/SQL-Manual/From-Join-Clause.md +++ b/src/zh/UserGuide/Master/Table/SQL-Manual/From-Join-Clause.md @@ -34,6 +34,7 @@ relation joinType : INNER? | FULL OUTER? + | CROSS? ; joinCriteria @@ -68,7 +69,11 @@ FROM 子句指定了查询操作的数据源。在逻辑上,查询的执行从 JOIN 用于将两个表基于某些条件连接起来,通常,连接条件是一个谓词,但也可以指定其他隐含的规则。 -在当前版本的 IoTDB 中,支持内连接(Inner Join)和全外连接(Full Outer Join),并且连接条件只能是时间列的等值连接。 +在当前版本的 IoTDB 中,支持以下连接方式: + +* 内连接(Inner Join),连接条件只能是时间列的等值连接。 +* 全外连接(Full Outer Join),连接条件可以是任意等值表达式。 +* 交叉连接(Cross Join) ### 3.1 内连接(Inner Join) @@ -115,6 +120,22 @@ SELECT selectExpr [, selectExpr] ... FROM [, ] ... [WHE 在当前版本的 IoTDB 中,只支持 FULL [OUTER] JOIN,即全外连接,返回左表和右表连接后的所有记录。如果某个表中的记录没有与另一个表中的记录匹配,则会返回 NULL 值。__FULL JOIN 只能使用显式连接方式。__ +SQL语法如下所示: + +```sql +// 在ON关键字后指定连接条件或在Using关键字后指定连接列 +SELECT selectExpr [, selectExpr] ... FROM FULL [OUTER] JOIN joinCriteria [WHERE whereCondition] + +joinCriteria + : ON booleanExpression + | USING '(' identifier (',' identifier)* ')' + ; +``` + +### 3.3 交叉连接(Cross Join) +交叉连接表示两个表的的笛卡尔积,返回左表N行记录和右表M行记录的N*M种组合。该种连接方式在实际中使用最少。 + + ## 4. 示例数据 在[示例数据页面](../Reference/Sample-Data.md)中,包含了用于构建表结构和插入数据的SQL语句,下载并在IoTDB CLI中执行这些语句,即可将数据导入IoTDB,您可以使用这些数据来测试和执行示例中的SQL语句,并获得相应的结果。 @@ -384,4 +405,92 @@ USING(time) +-----------------------------+-------+------------+-------+------------+ Total line number = 21 It costs 0.073s +``` + +示例3:连接条件为非time列 + +```sql +SELECT + region, + t1.time as time1, + t1.temperature as temperature1, + t2.time as time2, + t2.temperature as temperature2 +FROM + table1 t1 FULL JOIN table2 t2 +USING(region) +LIMIT 10 +``` + +查询结果: + +```sql ++------+-----------------------------+------------+-----------------------------+------------+ +|region| time1|temperature1| time2|temperature2| ++------+-----------------------------+------------+-----------------------------+------------+ +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-29T11:00:00.000+08:00| null| +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-28T08:00:00.000+08:00| 85.0| +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-30T00:00:00.000+08:00| 90.0| +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-29T00:00:00.000+08:00| 85.0| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-29T11:00:00.000+08:00| null| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-28T08:00:00.000+08:00| 85.0| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-30T00:00:00.000+08:00| 90.0| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-29T00:00:00.000+08:00| 85.0| +| 上海|2024-11-29T18:30:00.000+08:00| 90.0|2024-11-29T11:00:00.000+08:00| null| +| 上海|2024-11-29T18:30:00.000+08:00| 90.0|2024-11-28T08:00:00.000+08:00| 85.0| ++------+-----------------------------+------------+-----------------------------+------------+ +Total line number = 10 +It costs 0.040s +``` + +#### 4.2.3 Cross Join + +示例1: 显示连接 + +```sql +SELECT table1.*, table2.* FROM table1 CROSS JOIN table2 LIMIT 8; +``` + +查询结果: + +```sql ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-27T00:00:00.000+08:00| 北京| 1001| 101| B| 180| 85.0| 35.1| true|2024-11-27T16:37:01.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-28T08:00:00.000+08:00| 上海| 3001| 100| C| 90| 85.0| 35.2| false|2024-11-28T08:00:09.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-26T13:37:00.000+08:00| 北京| 1001| 100| A| 180| 90.0| 35.1| true|2024-11-26T13:37:34.000+08:00| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +Total line number = 8 +It costs 0.282s +``` + +示例2: 隐式连接 + +```sql +SELECT table1.*, table2.* FROM table1, table2 LIMIT 8; +``` + +查询结果: + +```sql ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-27T00:00:00.000+08:00| 北京| 1001| 101| B| 180| 85.0| 35.1| true|2024-11-27T16:37:01.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-28T08:00:00.000+08:00| 上海| 3001| 100| C| 90| 85.0| 35.2| false|2024-11-28T08:00:09.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-26T13:37:00.000+08:00| 北京| 1001| 100| A| 180| 90.0| 35.1| true|2024-11-26T13:37:34.000+08:00| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +Total line number = 8 +It costs 0.047s ``` \ No newline at end of file diff --git a/src/zh/UserGuide/latest-Table/SQL-Manual/From-Join-Clause.md b/src/zh/UserGuide/latest-Table/SQL-Manual/From-Join-Clause.md index b37633729..8a94550ef 100644 --- a/src/zh/UserGuide/latest-Table/SQL-Manual/From-Join-Clause.md +++ b/src/zh/UserGuide/latest-Table/SQL-Manual/From-Join-Clause.md @@ -34,6 +34,7 @@ relation joinType : INNER? | FULL OUTER? + | CROSS? ; joinCriteria @@ -68,7 +69,11 @@ FROM 子句指定了查询操作的数据源。在逻辑上,查询的执行从 JOIN 用于将两个表基于某些条件连接起来,通常,连接条件是一个谓词,但也可以指定其他隐含的规则。 -在当前版本的 IoTDB 中,支持内连接(Inner Join)和全外连接(Full Outer Join),并且连接条件只能是时间列的等值连接。 +在当前版本的 IoTDB 中,支持以下连接方式: + +* 内连接(Inner Join),连接条件只能是时间列的等值连接。 +* 全外连接(Full Outer Join),连接条件可以是任意等值表达式。 +* 交叉连接(Cross Join) ### 3.1 内连接(Inner Join) @@ -115,6 +120,22 @@ SELECT selectExpr [, selectExpr] ... FROM [, ] ... [WHE 在当前版本的 IoTDB 中,只支持 FULL [OUTER] JOIN,即全外连接,返回左表和右表连接后的所有记录。如果某个表中的记录没有与另一个表中的记录匹配,则会返回 NULL 值。__FULL JOIN 只能使用显式连接方式。__ +SQL语法如下所示: + +```sql +// 在ON关键字后指定连接条件或在Using关键字后指定连接列 +SELECT selectExpr [, selectExpr] ... FROM FULL [OUTER] JOIN joinCriteria [WHERE whereCondition] + +joinCriteria + : ON booleanExpression + | USING '(' identifier (',' identifier)* ')' + ; +``` + +### 3.3 交叉连接(Cross Join) +交叉连接表示两个表的的笛卡尔积,返回左表N行记录和右表M行记录的N*M种组合。该种连接方式在实际中使用最少。 + + ## 4. 示例数据 在[示例数据页面](../Reference/Sample-Data.md)中,包含了用于构建表结构和插入数据的SQL语句,下载并在IoTDB CLI中执行这些语句,即可将数据导入IoTDB,您可以使用这些数据来测试和执行示例中的SQL语句,并获得相应的结果。 @@ -384,4 +405,92 @@ USING(time) +-----------------------------+-------+------------+-------+------------+ Total line number = 21 It costs 0.073s +``` + +示例3:连接条件为非time列 + +```sql +SELECT + region, + t1.time as time1, + t1.temperature as temperature1, + t2.time as time2, + t2.temperature as temperature2 +FROM + table1 t1 FULL JOIN table2 t2 +USING(region) +LIMIT 10 +``` + +查询结果: + +```sql ++------+-----------------------------+------------+-----------------------------+------------+ +|region| time1|temperature1| time2|temperature2| ++------+-----------------------------+------------+-----------------------------+------------+ +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-29T11:00:00.000+08:00| null| +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-28T08:00:00.000+08:00| 85.0| +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-30T00:00:00.000+08:00| 90.0| +| 上海|2024-11-29T11:00:00.000+08:00| null|2024-11-29T00:00:00.000+08:00| 85.0| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-29T11:00:00.000+08:00| null| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-28T08:00:00.000+08:00| 85.0| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-30T00:00:00.000+08:00| 90.0| +| 上海|2024-11-30T09:30:00.000+08:00| 90.0|2024-11-29T00:00:00.000+08:00| 85.0| +| 上海|2024-11-29T18:30:00.000+08:00| 90.0|2024-11-29T11:00:00.000+08:00| null| +| 上海|2024-11-29T18:30:00.000+08:00| 90.0|2024-11-28T08:00:00.000+08:00| 85.0| ++------+-----------------------------+------------+-----------------------------+------------+ +Total line number = 10 +It costs 0.040s +``` + +#### 4.2.3 Cross Join + +示例1: 显示连接 + +```sql +SELECT table1.*, table2.* FROM table1 CROSS JOIN table2 LIMIT 8; +``` + +查询结果: + +```sql ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-27T00:00:00.000+08:00| 北京| 1001| 101| B| 180| 85.0| 35.1| true|2024-11-27T16:37:01.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-28T08:00:00.000+08:00| 上海| 3001| 100| C| 90| 85.0| 35.2| false|2024-11-28T08:00:09.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-26T13:37:00.000+08:00| 北京| 1001| 100| A| 180| 90.0| 35.1| true|2024-11-26T13:37:34.000+08:00| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +Total line number = 8 +It costs 0.282s +``` + +示例2: 隐式连接 + +```sql +SELECT table1.*, table2.* FROM table1, table2 LIMIT 8; +``` + +查询结果: + +```sql ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| time|region|plant_id|device_id|model_id|maintenance|temperature|humidity|status| arrival_time| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-27T00:00:00.000+08:00| 北京| 1001| 101| B| 180| 85.0| 35.1| true|2024-11-27T16:37:01.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-29T11:00:00.000+08:00| 上海| 3002| 100| E| 180| null| 45.1| true| null| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-28T08:00:00.000+08:00| 上海| 3001| 100| C| 90| 85.0| 35.2| false|2024-11-28T08:00:09.000+08:00| +|2024-11-30T09:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null|2024-11-26T13:37:00.000+08:00| 北京| 1001| 100| A| 180| 90.0| 35.1| true|2024-11-26T13:37:34.000+08:00| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-30T00:00:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 35.2| true| null| +|2024-11-30T14:30:00.000+08:00| 上海| 3002| 101| F| 360| 90.0| 34.8| true|2024-11-30T14:30:17.000+08:00|2024-11-29T00:00:00.000+08:00| 上海| 3001| 101| D| 360| 85.0| 35.1| null|2024-11-29T10:00:13.000+08:00| ++-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+-----------------------------+------+--------+---------+--------+-----------+-----------+--------+------+-----------------------------+ +Total line number = 8 +It costs 0.047s ``` \ No newline at end of file