-
Notifications
You must be signed in to change notification settings - Fork 1.2k
docs: add LATERAL derived tables documentation #21757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
qiancai
wants to merge
6
commits into
pingcap:master
Choose a base branch
from
qiancai:lateral-join-23123
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8f6e477
Create empty translation PR
qiancai 50d8e3d
Auto-sync: Update Chinese docs from English PR
github-actions[bot] d858057
revert the keyword doc change
qiancai fc18791
Discard changes to keywords.md
qiancai 7b504b1
Apply suggestions from code review
qiancai 085e7b4
Apply suggestions from code review
qiancai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| --- | ||
| title: 横向派生表 | ||
| summary: 了解 TiDB 中横向派生表 (`LATERAL` Derived Table) 的语法和当前限制。 | ||
| --- | ||
|
|
||
| # 横向派生表 | ||
|
|
||
| 横向派生表 (`LATERAL` Derived Table) 是 `FROM` 子句中的一种子查询,它可以引用同一个 `FROM` 子句中位于其前面的表中的列。相比而言,标准派生表中的子查询无法引用同一个 `FROM` 子句中其它表中的列,因此,横向派生表更加灵活。 | ||
|
qiancai marked this conversation as resolved.
Outdated
|
||
|
|
||
| 从 v8.5.7 和 v9.0.0 开始,TiDB 支持解析 `LATERAL` 派生表语法,语法与 MySQL 8.0 的语法([WL#8652](https://dev.mysql.com/worklog/task/?id=8652))一致。 | ||
|
qiancai marked this conversation as resolved.
Outdated
|
||
|
|
||
| > **注意:** | ||
| > | ||
| > 目前,TiDB 仅支持解析 `LATERAL` 派生表语法,尚不支持执行使用该语法的查询。如果你尝试执行此类查询,TiDB 将返回错误。你可以在 issue [#40328](https://github.com/pingcap/tidb/issues/40328) 中了解对该功能的完整执行支持的开发进展。 | ||
|
qiancai marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## 语法 | ||
|
|
||
| ```sql | ||
| SELECT ... FROM table_ref, LATERAL (subquery) [AS] alias [(col_list)] ... | ||
| SELECT ... FROM table_ref [LEFT] JOIN LATERAL (subquery) [AS] alias [(col_list)] ON ... | ||
|
qiancai marked this conversation as resolved.
Outdated
|
||
| ``` | ||
|
|
||
| - `LATERAL` 关键字必须位于派生表子查询之前。 | ||
| - 子查询右括号后必须指定表别名。 | ||
|
qiancai marked this conversation as resolved.
Outdated
|
||
| - 别名前的 `AS` 关键字是可选的。 | ||
| - 别名后可以跟一个可选的派生列列表,例如 `LATERAL (...) AS dt(col1, col2)`。 | ||
|
|
||
| ## 示例 | ||
|
|
||
| ### 使用逗号连接 `LATERAL` 派生表 | ||
|
|
||
| ```sql | ||
| SELECT * FROM t1, LATERAL (SELECT * FROM t2 WHERE t2.id = t1.id) AS dt; | ||
| ``` | ||
|
|
||
| 在此示例中,`t1` 和 `LATERAL` 派生表在同一个 `FROM` 子句中使用逗号连接。`LATERAL` 派生表中的子查询引用了位于其前面的表 `t1` 中的列 `t1.id`。普通派生表(不带 `LATERAL`)不支持此功能。 | ||
|
|
||
| ### 在 `LEFT JOIN` 中使用 `LATERAL` 派生表(带派生列列表) | ||
|
|
||
| ```sql | ||
| SELECT t1.id, dt.val | ||
| FROM t1 | ||
| LEFT JOIN LATERAL (SELECT t2.val FROM t2 WHERE t2.id = t1.id LIMIT 1) AS dt(val) | ||
| ON TRUE; | ||
| ``` | ||
|
|
||
| 在此示例中,`LATERAL` 派生表作为 `LEFT JOIN` 的右表,可以引用左表 `t1` 中的列 `t1.id`。派生列列表 `(val)` 会将子查询返回的列重命名为 `val`。 | ||
|
qiancai marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## 与标准派生表的比较 | ||
|
|
||
| | 功能 | 标准派生表 | 横向派生表 | | ||
| |---|---|---| | ||
| | 能否引用 `FROM` 子句中位于其前面的表中的列 | 否 | 是 | | ||
| | 是否必须指定别名 | 是 | 是 | | ||
| | 是否支持派生列列表 | 支持 | 支持 | | ||
|
|
||
| ## MySQL 兼容性 | ||
|
|
||
| TiDB 的 `LATERAL` 派生表语法与 MySQL 8.0 语法兼容。 | ||
|
|
||
| ## 另请参阅 | ||
|
|
||
| - [子查询相关的优化](/subquery-optimization.md) | ||
| - [关联子查询去关联](/correlated-subquery-optimization.md) | ||
| - [用 EXPLAIN 查看子查询的执行计划](/explain-subqueries.md) | ||
| - [MySQL 兼容性](/mysql-compatibility.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docs-cn/keywords.md
Line 396 in a01a708
The existing keyword.md doc already contains "LATERAL (R)", so there is no need to add it in this PR.