Skip to content

Commit 58e3cd6

Browse files
Auto-sync: Update Chinese docs from English PR
Synced from: pingcap/docs#22583 Target PR: pingcap#21613 AI Provider: azure Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent b6270f4 commit 58e3cd6

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

TOC.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@
269269
- [子查询相关的优化](/subquery-optimization.md)
270270
- [列裁剪](/column-pruning.md)
271271
- [关联子查询去关联](/correlated-subquery-optimization.md)
272+
- [LATERAL 派生表](/lateral-derived-tables.md)
272273
- [Max/Min 消除](/max-min-eliminate.md)
273274
- [谓词下推](/predicate-push-down.md)
274275
- [分区裁剪](/partition-pruning.md)

lateral-derived-tables.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
---
2+
title: LATERAL 派生表
3+
summary: 了解 TiDB 中 LATERAL 派生表的语法和当前限制。
4+
---
5+
6+
# LATERAL 派生表
7+
8+
**lateral derived table**`FROM` 子句中的一种子查询,它可以引用同一 `FROM` 子句中更早出现的表的列。这使它比标准派生表更强大,因为标准派生表中的子查询不能引用同一 `FROM` 子句中的外部列。
9+
10+
TiDB 可识别用于派生表的 `LATERAL` 语法,遵循 MySQL 8.0 的语法([WL#8652](https://dev.mysql.com/worklog/task/?id=8652))。
11+
12+
> **Note:**
13+
>
14+
> 当前,TiDB 支持解析 `LATERAL` 派生表语法,但尚不支持执行,执行时会返回错误。你可以在 issue [#40328](https://github.com/pingcap/tidb/issues/40328) 中跟踪完整执行支持的进展。
15+
16+
## 语法
17+
18+
```sql
19+
SELECT ... FROM table_ref, LATERAL (subquery) [AS] alias [(col_list)] ...
20+
SELECT ... FROM table_ref [LEFT] JOIN LATERAL (subquery) [AS] alias [(col_list)] ON ...
21+
```
22+
23+
- `LATERAL` 关键字必须位于派生表子查询之前。
24+
- 子查询右括号后必须提供表别名。
25+
- 别名前的 `AS` 关键字是可选的。
26+
- 别名后可以跟一个可选的派生列列表:`LATERAL (...) AS dt(col1, col2)`
27+
28+
## 示例
29+
30+
### 逗号连接
31+
32+
```sql
33+
SELECT * FROM t1, LATERAL (SELECT * FROM t2 WHERE t2.id = t1.id) AS dt;
34+
```
35+
36+
在此示例中,子查询引用了前一个表 `t1` 中的列 `t1.id`。普通派生表(不带 `LATERAL`)无法做到这一点。
37+
38+
### 带派生列列表的 LEFT JOIN
39+
40+
```sql
41+
SELECT t1.id, dt.val
42+
FROM t1
43+
LEFT JOIN LATERAL (SELECT t2.val FROM t2 WHERE t2.id = t1.id LIMIT 1) AS dt(val)
44+
ON TRUE;
45+
```
46+
47+
派生列列表 `(val)` 会重命名子查询返回的列。
48+
49+
## 与标准派生表的比较
50+
51+
| Feature | Standard derived table | LATERAL derived table |
52+
|---|---|---|
53+
| Can reference outer `FROM` columns | No | Yes |
54+
| Alias required | Yes | Yes |
55+
| Derived column list | Supported | Supported |
56+
57+
## MySQL 兼容性
58+
59+
TiDB 的 LATERAL 派生表语法在语法级别上与 MySQL 8.0 兼容。
60+
61+
## 另请参阅
62+
63+
- [Subquery Related Optimizations](/subquery-optimization.md)
64+
- [Decorrelation of Correlated Subquery](/correlated-subquery-optimization.md)
65+
- [Explain Statements That Use Subqueries](/explain-subqueries.md)
66+
- [MySQL Compatibility](/mysql-compatibility.md)

mysql-compatibility.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ TiDB 高度兼容 MySQL 协议,以及 MySQL 5.7 和 MySQL 8.0 常用的功能
4747
* "Session Tracker: 将 GTID 上下文信息添加到 OK 包中"
4848
* 降序索引 [#2519](https://github.com/pingcap/tidb/issues/2519)
4949
* `SKIP LOCKED` 语法 [#18207](https://github.com/pingcap/tidb/issues/18207)
50-
* 横向派生表 [#40328](https://github.com/pingcap/tidb/issues/40328)
50+
* 横向派生表(语法已被识别,但尚不支持执行)[#40328](https://github.com/pingcap/tidb/issues/40328)。有关语法的详细信息,请参阅 [LATERAL Derived Tables](/lateral-derived-tables.md)
5151
* JOIN 的 ON 子句的子查询 [#11414](https://github.com/pingcap/tidb/issues/11414)
5252

5353
## 与 MySQL 有差异的特性详细说明

0 commit comments

Comments
 (0)