-
Notifications
You must be signed in to change notification settings - Fork 1.2k
docs: add LATERAL derived tables documentation #21613
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
2
commits into
pingcap:master
Choose a base branch
from
qiancai:feat/lateral-derived-tables-docs-22583
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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: LATERAL 派生表 | ||
| summary: 了解 TiDB 中 LATERAL 派生表的语法和当前限制。 | ||
| --- | ||
|
|
||
| # LATERAL 派生表 | ||
|
|
||
| **LATERAL 派生表**是 `FROM` 子句中的一种子查询,它可以引用同一个 `FROM` 子句中位于其前面的表中的列。与标准派生表相比,LATERAL 派生表功能更强,因为标准派生表中的子查询不支持引用同一个 `FROM` 子句中的外部列。 | ||
|
|
||
| TiDB 可识别用于派生表的 `LATERAL` 语法,该语法遵循 MySQL 8.0 语法([WL#8652](https://dev.mysql.com/worklog/task/?id=8652))。 | ||
|
|
||
| > **注意:** | ||
| > | ||
| > 当前,TiDB 支持解析 `LATERAL` 派生表语法,但尚不支持执行该语法,执行时会返回错误。你可以通过 issue [#40328](https://github.com/pingcap/tidb/issues/40328) 跟踪完整执行支持的进展。 | ||
|
|
||
| ## 语法 | ||
|
|
||
| ```sql | ||
| SELECT ... FROM table_ref, LATERAL (subquery) [AS] alias [(col_list)] ... | ||
| SELECT ... FROM table_ref [LEFT] JOIN LATERAL (subquery) [AS] alias [(col_list)] ON ... | ||
| ``` | ||
|
|
||
| - `LATERAL` 关键字必须位于派生表子查询之前。 | ||
| - 子查询右括号后必须指定表别名。 | ||
| - 别名前的 `AS` 关键字是可选的。 | ||
| - 别名后可以指定可选的派生列的列表:`LATERAL (...) AS dt(col1, col2)`。 | ||
|
|
||
| ## 示例 | ||
|
|
||
| ### 逗号连接 | ||
|
|
||
| ```sql | ||
| SELECT * FROM t1, LATERAL (SELECT * FROM t2 WHERE t2.id = t1.id) AS dt; | ||
| ``` | ||
|
|
||
| 在此示例中,子查询引用了前一个表 `t1` 中的列 `t1.id`。普通派生表(不带 `LATERAL`)不能这样引用前面的表。 | ||
|
|
||
| ### 带派生列列表的 LEFT JOIN | ||
|
|
||
| ```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; | ||
| ``` | ||
|
|
||
| 派生列列表 `(val)` 将会重命名子查询返回的列。 | ||
|
|
||
| ## 与标准派生表的比较 | ||
|
|
||
| | 功能 | 标准派生表 | LATERAL 派生表 | | ||
| |---|---|---| | ||
| | 是否可以引用外部 `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.
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.