共計 3829 個字符,預計需要花費 10 分鐘才能閱讀完成。
這篇文章主要介紹了 PostgreSQL 如何實現上拉子鏈接,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓丸趣 TV 小編帶著大家一起了解一下。
按官方文檔的介紹, 子鏈接 Sublink 代表的是出現在表達式 (可能會出現組合運算符) 中的子查詢, 子查詢的類型包括:
EXISTS_SUBLINK
語法:EXISTS(SELECT …)
select *
from t_dwxx a
where exists (select b.dwbh from t_grxx b where a.dwbh = b.dwbh);
ALL_SUBLINK
語法:(lefthand) op ALL (SELECT …)
select *
from t_dwxx a
where dwbh all (select b.dwbh from t_grxx b);
ANY_SUBLINK
語法:(lefthand) op ANY (SELECT …)
select *
from t_dwxx a
where dwbh = any (select b.dwbh from t_grxx b);
ROWCOMPARE_SUBLINK
語法:(lefthand) op (SELECT …)
select *
from t_dwxx a
where dwbh (select max(b.dwbh) from t_grxx b);
EXPR_SUBLINK
語法:(SELECT with single targetlist item …)
select *,(select max(dwbh) from t_grxx)
from t_dwxx a;
MULTIEXPR_SUBLINK
語法:(SELECT with multiple targetlist items …)
ARRAY_SUBLINK
語法:ARRAY(SELECT with single targetlist item …)
CTE_SUBLINK
語法:
WITH query (never actually part of an expression)
官方說明:
/*
* SubLink
*
* A SubLink represents a subselect appearing in an expression, and in some
* cases also the combining operator(s) just above it. The subLinkType
* indicates the form of the expression represented:
* EXISTS_SUBLINK EXISTS(SELECT ...)
* ALL_SUBLINK (lefthand) op ALL (SELECT ...)
* ANY_SUBLINK (lefthand) op ANY (SELECT ...)
* ROWCOMPARE_SUBLINK (lefthand) op (SELECT ...)
* EXPR_SUBLINK (SELECT with single targetlist item ...)
* MULTIEXPR_SUBLINK (SELECT with multiple targetlist items ...)
* ARRAY_SUBLINK ARRAY(SELECT with single targetlist item ...)
* CTE_SUBLINK WITH query (never actually part of an expression)
* For ALL, ANY, and ROWCOMPARE, the lefthand is a list of expressions of the
* same length as the subselect s targetlist. ROWCOMPARE will *always* have
* a list with more than one entry; if the subselect has just one target
* then the parser will create an EXPR_SUBLINK instead (and any operator
* above the subselect will be represented separately).
* ROWCOMPARE, EXPR, and MULTIEXPR require the subselect to deliver at most
* one row (if it returns no rows, the result is NULL).
* ALL, ANY, and ROWCOMPARE require the combining operators to deliver boolean
* results. ALL and ANY combine the per-row results using AND and OR
* semantics respectively.
* ARRAY requires just one target column, and creates an array of the target
* column s type using any number of rows resulting from the subselect.
*
* 子鏈接屬于 Expr Node, 但并不意味著子鏈接是可以執行的. 子鏈接必須在計劃期間通過子計劃節點在表達式樹中替換
* SubLink is classed as an Expr node, but it is not actually executable;
* it must be replaced in the expression tree by a SubPlan node during
* planning.
*
* NOTE: in the raw output of gram.y, testexpr contains just the raw form
* of the lefthand expression (if any), and operName is the String name of
* the combining operator. Also, subselect is a raw parsetree. During parse
* analysis, the parser transforms testexpr into a complete boolean expression
* that compares the lefthand value(s) to PARAM_SUBLINK nodes representing the
* output columns of the subselect. And subselect is transformed to a Query.
* This is the representation seen in saved rules and in the rewriter.
*
* In EXISTS, EXPR, MULTIEXPR, and ARRAY SubLinks, testexpr and operName
* are unused and are always null.
*
* subLinkId is currently used only for MULTIEXPR SubLinks, and is zero in
* other SubLinks. This number identifies different multiple-assignment
* subqueries within an UPDATE statement s SET list. It is unique only
* within a particular targetlist. The output column(s) of the MULTIEXPR
* are referenced by PARAM_MULTIEXPR Params appearing elsewhere in the tlist.
*
* The CTE_SUBLINK case never occurs in actual SubLink nodes, but it is used
* in SubPlans generated for WITH subqueries.
*/
typedef enum SubLinkType
EXISTS_SUBLINK,
ALL_SUBLINK,
ANY_SUBLINK,
ROWCOMPARE_SUBLINK,
EXPR_SUBLINK,
MULTIEXPR_SUBLINK,
ARRAY_SUBLINK,
CTE_SUBLINK /* for SubPlans only */
} SubLinkType;
正文完