Skip to content

Commit

Permalink
Merge pull request #1176 from yangj1211/apply
Browse files Browse the repository at this point in the history
add doc of apply
  • Loading branch information
yangj1211 authored Dec 9, 2024
2 parents 0fab9f2 + 6b6ec5d commit 4926ff2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# **CROSS APPLY**

## **语法说明**

`CROSS APPLY` 是 MatrixOne 中的一个特殊连接操作符,主要用于将一个表的每一行与另一个返回结果集的表函数(如表值函数)进行连接。与 JOIN 不同,CROSS APPLY 允许右侧的子查询或表值函数依赖于左侧表的每一行,从而为每一行返回不同的结果。

## **语法结构**

```
> SELECT <columns>
FROM <table_name>
CROSS APPLY <table_function> <alias>;
```

## **示例**

```sql
mysql> create table t1(a int, b int);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into t1 values(1,3),(1,-1);
Query OK, 2 rows affected (0.00 sec)

mysql> select * from t1 cross apply generate_series(t1.a,t1.b,1)g;
+------+------+--------+
| a | b | result |
+------+------+--------+
| 1 | 3 | 1 |
| 1 | 3 | 2 |
| 1 | 3 | 3 |
+------+------+--------+
3 rows in set (0.02 sec)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# **OUTER APPLY**

## **语法说明**

`OUTER APPLY` 是用来将主表的每一行与一个函数或子查询的结果动态关联的。(右侧内容可以基于主表的行动态生成)。与 [`CROSS APPLY`](./cross-apply.md) 不同的是,即使右侧没有数据,也不会丢弃主表的记录,只是右侧列用 NULL 填充。(类似 LEFT JOIN 的效果,但右侧是动态生成的数据)。

## **语法结构**

```
> SELECT <columns>
FROM <table_name>
outer APPLY <table_function><alias>;
```

## **示例**

```sql
mysql> create table t1(a int, b int);
Query OK, 0 rows affected (0.03 sec)

mysql> insert into t1 values(1,3),(1,-1);
Query OK, 2 rows affected (0.00 sec)

mysql> select * from t1 outer apply generate_series(t1.a,t1.b,1)g;
+------+------+--------+
| a | b | result |
+------+------+--------+
| 1 | 3 | 1 |
| 1 | 3 | 2 |
| 1 | 3 | 3 |
| 1 | -1 | NULL |
+------+------+--------+
4 rows in set (0.01 sec)
```
5 changes: 5 additions & 0 deletions docs/MatrixOne/Reference/SQL-Reference/SQL-Type.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@
- [OUTER JOIN](Data-Query-Language/join/outer-join.md)
- [NATURAL JOIN](Data-Query-Language/join/natural-join.md)

### 应用查询(Apply Query),将某个操作应用于每一行数据的查询方式

- [CROSS APPLY](Data-Query-Language/apply/cross-apply.md)
- [OUTER APPLY](Data-Query-Language/apply/outer-apply.md)

### 公用表达式(Common Table Expressions),将某些查询作为是临时结果,可以在其他 SQL 中引用,如 SELECT, INSERT, UPDATE 和 DELETE,其仅存在于查询执行期间

- [With CTE](Data-Query-Language/with-cte.md)
Expand Down
21 changes: 12 additions & 9 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,9 @@ nav:
- LAST_QUERY_ID(): MatrixOne/Reference/SQL-Reference/Data-Manipulation-Language/information-functions/last-query-id.md
- LAST_INSERT_ID(): MatrixOne/Reference/SQL-Reference/Data-Manipulation-Language/information-functions/last-insert-id.md
- 数据查询语言(DQL):
- SELECT: MatrixOne/Reference/SQL-Reference/Data-Query-Language/select.md
- SUBQUERY:
- SUBQUERY 概述: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/subquery.md
- Derived Tables: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/derived-tables.md
- 子查询与比较操作符的使用: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/comparisons-using-subqueries.md
- SUBQUERY with ANY or SOME: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/subquery-with-any-some.md
- SUBQUERY with ALL: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/subquery-with-all.md
- SUBQUERY with EXISTS: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/subquery-with-exists.md
- SUBQUERY with IN: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/subquery-with-in.md
- APPLY:
- CROSS APPLY: MatrixOne/Reference/SQL-Reference/Data-Query-Language/apply/cross-apply.md
- OUTER APPLY: MatrixOne/Reference/SQL-Reference/Data-Query-Language/apply/outer-apply.md
- JOIN:
- JOIN 概述: MatrixOne/Reference/SQL-Reference/Data-Query-Language/join/join.md
- CROSS JOIN: MatrixOne/Reference/SQL-Reference/Data-Query-Language/join/cross-join.md
Expand All @@ -402,6 +396,15 @@ nav:
- FULL JOIN: MatrixOne/Reference/SQL-Reference/Data-Query-Language/join/full-join.md
- OUTER JOIN: MatrixOne/Reference/SQL-Reference/Data-Query-Language/join/outer-join.md
- NATURAL JOIN: MatrixOne/Reference/SQL-Reference/Data-Query-Language/join/natural-join.md
- SELECT: MatrixOne/Reference/SQL-Reference/Data-Query-Language/select.md
- SUBQUERY:
- SUBQUERY 概述: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/subquery.md
- Derived Tables: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/derived-tables.md
- 子查询与比较操作符的使用: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/comparisons-using-subqueries.md
- SUBQUERY with ANY or SOME: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/subquery-with-any-some.md
- SUBQUERY with ALL: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/subquery-with-all.md
- SUBQUERY with EXISTS: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/subquery-with-exists.md
- SUBQUERY with IN: MatrixOne/Reference/SQL-Reference/Data-Query-Language/subqueries/subquery-with-in.md
- With CTE: MatrixOne/Reference/SQL-Reference/Data-Query-Language/with-cte.md
- 联合查询:
- 联合查询概述: MatrixOne/Reference/SQL-Reference/Data-Query-Language/union-intersect-minus-overview.md
Expand Down

0 comments on commit 4926ff2

Please sign in to comment.