class Select(HasPrefixes, HasSuffixes, HasHints, HasCompileState, _SelectFromElements, GenerativeSelect, TypedReturnsRows[
Represents a ``SELECT`` statement. The :class:`_sql.Select` object is normally constructed using the :func:`_sql.select` function. See that function for details. .. seealso:: :func:`_sql.select` :ref:`tutorial_selecting_data` - in the 2.0 tutorial
Method | __init__ |
Construct a new :class:`_expression.Select`. |
Method | add |
Return a new :func:`_expression.select` construct with the given entities appended to its columns clause. |
Method | column |
Return a new :func:`_expression.select` construct with the given column expression added to its columns clause. |
Method | correlate |
Return a new :class:`_expression.Select` which will correlate the given FROM clauses to that of an enclosing :class:`_expression.Select`. |
Method | correlate |
Return a new :class:`_expression.Select` which will omit the given FROM clauses from the auto-correlation process. |
Method | distinct |
Return a new :func:`_expression.select` construct which will apply DISTINCT to its columns clause. |
Method | except_ |
Return a SQL ``EXCEPT`` of this select() construct against the given selectable provided as positional arguments. |
Method | except |
Return a SQL ``EXCEPT ALL`` of this select() construct against the given selectables provided as positional arguments. |
Method | filter |
A synonym for the :meth:`_sql.Select.where` method. |
Method | filter |
apply the given filtering criterion as a WHERE clause to this select. |
Method | from |
Apply the columns which this :class:`.Select` would select onto another statement. |
Method | get |
Return immediate child :class:`.visitors.HasTraverseInternals` elements of this :class:`.visitors.HasTraverseInternals`. |
Method | get |
Compute the final displayed list of :class:`_expression.FromClause` elements. |
Method | having |
Return a new :func:`_expression.select` construct with the given expression added to its HAVING clause, joined to the existing clause via AND, if any. |
Method | intersect |
Return a SQL ``INTERSECT`` of this select() construct against the given selectables provided as positional arguments. |
Method | intersect |
Return a SQL ``INTERSECT ALL`` of this select() construct against the given selectables provided as positional arguments. |
Method | is |
Return ``True`` if this :class:`.ReturnsRows` is 'derived' from the given :class:`.FromClause`. |
Method | join |
Create a SQL JOIN against this :class:`_expression.Select` object's criterion and apply generatively, returning the newly resulting :class:`_expression.Select`. |
Method | join |
Create a SQL JOIN against this :class:`_expression.Select` object's criterion and apply generatively, returning the newly resulting :class:`_expression.Select`. |
Method | outerjoin |
Create a left outer join. |
Method | outerjoin |
Create a SQL LEFT OUTER JOIN against this :class:`_expression.Select` object's criterion and apply generatively, returning the newly resulting :class:`_expression.Select`. |
Method | reduce |
Return a new :func:`_expression.select` construct with redundantly named, equivalently-valued columns removed from the columns clause. |
Method | scalar |
Return a 'scalar' representation of this selectable, which can be used as a column expression. |
Method | select |
Return a new :func:`_expression.select` construct with the given FROM expression(s) merged into its list of FROM objects. |
Method | selected |
A :class:`_expression.ColumnCollection` representing the columns that this SELECT statement or similar construct returns in its result set, not including :class:`_sql.TextClause` constructs. |
Method | self |
Apply a 'grouping' to this :class:`_expression.ClauseElement`. |
Method | union |
Return a SQL ``UNION`` of this select() construct against the given selectables provided as positional arguments. |
Method | union |
Return a SQL ``UNION ALL`` of this select() construct against the given selectables provided as positional arguments. |
Method | where |
Return a new :func:`_expression.select` construct with the given expression added to its WHERE clause, joined to the existing clause via AND, if any. |
Method | with |
Return a new :func:`_expression.select` construct with its columns clause replaced with the given entities. |
Class Variable | __visit |
Undocumented |
Property | column |
Return a :term:`plugin-enabled` 'column descriptions' structure referring to the columns which are SELECTed by this statement. |
Property | columns |
Return the set of :class:`_expression.FromClause` objects implied by the columns clause of this SELECT statement. |
Property | froms |
Return the displayed list of :class:`_expression.FromClause` elements. |
Property | inner |
An iterator of all :class:`_expression.ColumnElement` expressions which would be rendered into the columns clause of the resulting SELECT statement. |
Property | whereclause |
Return the completed WHERE clause for this :class:`_expression.Select` statement. |
Class Method | _create |
Create a :class:`.Select` using raw ``__new__`` with no coercions. |
Method | _all |
A sequence of expressions that correspond to what is rendered in the columns clause, including :class:`_sql.TextClause` constructs. |
Method | _copy |
Reassign internal elements to be clones of themselves. |
Method | _ensure |
Ensure that the names generated by this selectbase will be disambiguated in some way, if possible. |
Method | _filter |
Undocumented |
Method | _generate |
Generate column proxies to place in the exported ``.c`` collection of a subquery. |
Method | _needs |
Undocumented |
Method | _scalar |
Undocumented |
Method | _set |
Undocumented |
Class Variable | _cache |
Undocumented |
Class Variable | _compile |
Undocumented |
Class Variable | _having |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _memoized |
Undocumented |
Class Variable | _setup |
Undocumented |
Class Variable | _traverse |
Undocumented |
Class Variable | _where |
Undocumented |
Instance Variable | _auto |
Undocumented |
Instance Variable | _correlate |
Undocumented |
Instance Variable | _correlate |
Undocumented |
Instance Variable | _distinct |
Undocumented |
Instance Variable | _distinct |
Undocumented |
Instance Variable | _from |
Undocumented |
Instance Variable | _raw |
Undocumented |
Inherited from HasPrefixes
:
Method | prefix |
Add one or more expressions following the statement keyword, i.e. SELECT, INSERT, UPDATE, or DELETE. Generative. |
Class Variable | _has |
Undocumented |
Instance Variable | _prefixes |
Undocumented |
Inherited from HasSuffixes
(via HasPrefixes
):
Method | suffix |
Add one or more expressions following the statement as a whole. |
Class Variable | _has |
Undocumented |
Instance Variable | _suffixes |
Undocumented |
Inherited from HasHints
(via HasPrefixes
, HasSuffixes
):
Method | with |
Add an indexing or other executional context hint for the given selectable to this :class:`_expression.Select` or other selectable object. |
Method | with |
Add a statement hint to this :class:`_expression.Select` or other selectable object. |
Method | _with |
Undocumented |
Class Variable | _has |
Undocumented |
Class Variable | _hints |
Undocumented |
Class Variable | _statement |
Undocumented |
Inherited from HasCompileState
(via HasPrefixes
, HasSuffixes
, HasHints
):
Class Variable | _compile |
Undocumented |
Inherited from _SelectFromElements
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
):
Class Variable | __slots__ |
Undocumented |
Method | _iterate |
Undocumented |
Inherited from GenerativeSelect
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
):
Method | fetch |
Return a new selectable with the given FETCH FIRST criterion applied. |
Method | get |
Retrieve the current label style. |
Method | group |
Return a new selectable with the given list of GROUP BY criterion applied. |
Method | limit |
Return a new selectable with the given LIMIT criterion applied. |
Method | offset |
Return a new selectable with the given OFFSET criterion applied. |
Method | order |
Return a new selectable with the given list of ORDER BY criteria applied. |
Method | set |
Return a new selectable with the specified label style. |
Method | slice |
Apply LIMIT / OFFSET to this statement based on a slice. |
Method | with |
Specify a ``FOR UPDATE`` clause for this :class:`_expression.GenerativeSelect`. |
Method | _offset |
Convert the given value to an "offset or limit" clause. |
Method | _offset |
Convert the "offset or limit" clause of a select construct to an integer. |
Method | _simple |
True if the clause is a simple integer, False if it is not present or is a SQL expression. |
Instance Variable | _fetch |
Undocumented |
Instance Variable | _fetch |
Undocumented |
Instance Variable | _for |
Undocumented |
Instance Variable | _group |
Undocumented |
Instance Variable | _label |
Undocumented |
Instance Variable | _limit |
Undocumented |
Instance Variable | _offset |
Undocumented |
Instance Variable | _order |
Undocumented |
Property | _group |
ClauseList access to group_by_clauses for legacy dialects |
Property | _has |
Undocumented |
Property | _limit |
Get an integer value for the limit. This should only be used by code that cannot support a limit as a BindParameter or other custom clause as it will throw an exception if the limit isn't currently set to an integer. |
Property | _offset |
Get an integer value for the offset. This should only be used by code that cannot support an offset as a BindParameter or other custom clause as it will throw an exception if the offset isn't currently set to an integer. |
Property | _order |
ClauseList access to order_by_clauses for legacy dialects |
Inherited from SelectBase
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
):
Method | alias |
Return a named subquery against this :class:`_expression.SelectBase`. |
Method | as |
Undocumented |
Method | exists |
Return an :class:`_sql.Exists` representation of this selectable, which can be used as a column expression. |
Method | label |
Return a 'scalar' representation of this selectable, embedded as a subquery with a label. |
Method | lateral |
Return a LATERAL alias of this :class:`_expression.Selectable`. |
Method | select |
Undocumented |
Method | subquery |
Return a subquery of this :class:`_expression.SelectBase`. |
Class Variable | is |
Undocumented |
Property | c |
Undocumented |
Property | columns |
Undocumented |
Property | exported |
A :class:`_expression.ColumnCollection` that represents the "exported" columns of this :class:`_expression.Selectable`, not including :class:`_sql.TextClause` constructs. |
Method | _implicit |
Undocumented |
Method | _refresh |
reset internal collections for an incoming column being added. |
Class Variable | _is |
Undocumented |
Inherited from SelectStatementRole
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
):
Class Variable | _role |
Undocumented |
Inherited from Executable
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
):
Method | execution |
Set non-SQL options for the statement which take effect during execution. |
Method | get |
Get the non-SQL options which will take effect during execution. |
Method | options |
Apply options to this statement. |
Class Variable | is |
Undocumented |
Class Variable | is |
Undocumented |
Class Variable | is |
Undocumented |
Class Variable | is |
Undocumented |
Class Variable | is |
Undocumented |
Class Variable | supports |
Undocumented |
Method | _add |
Add a context option to this statement. |
Method | _compile |
Undocumented |
Method | _execute |
Undocumented |
Method | _execute |
Undocumented |
Method | _set |
Assign the compile options to a new value. |
Method | _update |
update the _compile_options with new keys. |
Class Variable | _executable |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _with |
Undocumented |
Class Variable | _with |
Undocumented |
Instance Variable | _compile |
Undocumented |
Instance Variable | _execution |
Undocumented |
Property | _effective |
Undocumented |
Inherited from StatementRole
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
):
Property | _propagate |
Undocumented |
Inherited from AllowsLambdaRole
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
):
Class Variable | allows |
Undocumented |
Inherited from HasCTE
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
):
Method | add |
Add one or more :class:`_sql.CTE` constructs to this statement. |
Method | cte |
Return a new :class:`_expression.CTE`, or Common Table Expression instance. |
Class Variable | _has |
Undocumented |
Class Variable | _independent |
Undocumented |
Class Variable | _independent |
Undocumented |
Inherited from SelectsRows
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
):
Method | _generate |
Generate column names as rendered in a SELECT statement by the compiler. |
Inherited from SupportsCloneAnnotations
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
, SelectsRows
):
Method | _annotate |
return a copy of this ClauseElement with annotations updated by the given dictionary. |
Method | _deannotate |
return a copy of this :class:`_expression.ClauseElement` with annotations removed. |
Method | _with |
return a copy of this ClauseElement with annotations replaced by the given dictionary. |
Class Variable | _clone |
Undocumented |
Inherited from Selectable
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
, SelectsRows
, SupportsCloneAnnotations
):
Method | corresponding |
Given a :class:`_expression.ColumnElement`, return the exported :class:`_expression.ColumnElement` object from the :attr:`_expression.Selectable.exported_columns` collection of this :class:`_expression... |
Method | replace |
Replace all occurrences of :class:`_expression.FromClause` 'old' with the given :class:`_expression.Alias` object, returning a copy of this :class:`_expression.FromClause`. |
Class Variable | is |
Undocumented |
Inherited from ReturnsRows
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
, SelectsRows
, SupportsCloneAnnotations
, Selectable
):
Property | selectable |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Inherited from SQLRole
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
, SelectsRows
, SupportsCloneAnnotations
, Selectable
, ReturnsRows
, ReturnsRowsRole
):
Class Variable | uses |
Undocumented |
Inherited from DQLDMLClauseElement
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
, SelectsRows
, SupportsCloneAnnotations
, Selectable
, ReturnsRows
, ReturnsRowsRole
, SQLRole
):
Method | compile |
Compile this SQL expression. |
Method | _compiler |
Return a compiler appropriate for this ClauseElement, given a Dialect. |
Inherited from ClauseElement
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
, SelectsRows
, SupportsCloneAnnotations
, Selectable
, ReturnsRows
, ReturnsRowsRole
, SQLRole
, DQLDMLClauseElement
):
Method | __bool__ |
Undocumented |
Method | __getstate__ |
Undocumented |
Method | __invert__ |
Undocumented |
Method | __repr__ |
Undocumented |
Method | compare |
Compare this :class:`_expression.ClauseElement` to the given :class:`_expression.ClauseElement`. |
Method | params |
Return a copy with :func:`_expression.bindparam` elements replaced. |
Method | unique |
Return a copy with :func:`_expression.bindparam` elements replaced. |
Class Variable | is |
Undocumented |
Class Variable | negation |
Undocumented |
Property | description |
Undocumented |
Property | entity |
Undocumented |
Method | _clone |
Create a shallow copy of this ClauseElement. |
Method | _cloned |
Return the set consisting all cloned ancestors of this ClauseElement. |
Method | _negate |
Undocumented |
Method | _negate |
a hook to allow the right side of a binary expression to respond to a negation of the binary expression. |
Method | _replace |
Undocumented |
Method | _set |
Undocumented |
Method | _ungroup |
Return this :class:`_expression.ClauseElement` without any groupings. |
Method | _with |
in the context of binary expression, convert the type of this object to the one given. |
Class Variable | _gen |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Class Variable | _is |
Undocumented |
Property | _constructor |
return the 'constructor' for this ClauseElement. |
Property | _from |
Undocumented |
Property | _order |
Undocumented |
Inherited from SupportsAnnotations
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
, SelectsRows
, SupportsCloneAnnotations
, Selectable
, ReturnsRows
, ReturnsRowsRole
, SQLRole
, DQLDMLClauseElement
, ClauseElement
, SupportsWrappingAnnotations
):
Class Variable | proxy |
Undocumented |
Method | _gen |
Undocumented |
Property | _annotations |
Undocumented |
Inherited from MemoizedHasCacheKey
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
, SelectsRows
, SupportsCloneAnnotations
, Selectable
, ReturnsRows
, ReturnsRowsRole
, SQLRole
, DQLDMLClauseElement
, ClauseElement
, SupportsWrappingAnnotations
, SupportsAnnotations
):
Method | _generate |
return a cache key. |
Inherited from HasCacheKey
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
, SelectsRows
, SupportsCloneAnnotations
, Selectable
, ReturnsRows
, ReturnsRowsRole
, SQLRole
, DQLDMLClauseElement
, ClauseElement
, SupportsWrappingAnnotations
, SupportsAnnotations
, MemoizedHasCacheKey
):
Class Variable | inherit |
Indicate if this :class:`.HasCacheKey` instance should make use of the cache key generation scheme used by its immediate superclass. |
Class Method | _generate |
generate cache key dispatcher for a new class. |
Class Method | _generate |
Undocumented |
Method | _gen |
return an optional cache key. |
Class Variable | _generated |
Undocumented |
Class Variable | _hierarchy |
private attribute which may be set to False to prevent the inherit_cache warning from being emitted for a hierarchy of subclasses. |
Class Variable | _is |
Undocumented |
Inherited from Generative
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
, SelectsRows
, SupportsCloneAnnotations
, Selectable
, ReturnsRows
, ReturnsRowsRole
, SQLRole
, DQLDMLClauseElement
, ClauseElement
, SupportsWrappingAnnotations
, SupportsAnnotations
, MemoizedHasCacheKey
, HasCacheKey
):
Method | _generate |
Undocumented |
Inherited from CompilerElement
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
, SelectsRows
, SupportsCloneAnnotations
, Selectable
, ReturnsRows
, ReturnsRowsRole
, SQLRole
, DQLDMLClauseElement
, ClauseElement
, SupportsWrappingAnnotations
, SupportsAnnotations
, MemoizedHasCacheKey
, HasCacheKey
, Generative
, HasCopyInternals
, ExternallyTraversible
, HasTraverseInternals
):
Method | __str__ |
Undocumented |
Class Variable | stringify |
Undocumented |
Inherited from Visitable
(via HasPrefixes
, HasSuffixes
, HasHints
, HasCompileState
, _SelectFromElements
, GenerativeSelect
, SelectBase
, SelectStatementRole
, TypedReturnsRows
, ExecutableReturnsRows
, Executable
, StatementRole
, DMLSelectRole
, CompoundElementRole
, AllowsLambdaRole
, InElementRole
, HasCTE
, HasCTERole
, SelectsRows
, SupportsCloneAnnotations
, Selectable
, ReturnsRows
, ReturnsRowsRole
, SQLRole
, DQLDMLClauseElement
, ClauseElement
, SupportsWrappingAnnotations
, SupportsAnnotations
, MemoizedHasCacheKey
, HasCacheKey
, Generative
, HasCopyInternals
, ExternallyTraversible
, HasTraverseInternals
, CompilerElement
):
Method | __class |
Undocumented |
Method | __init |
Undocumented |
Class Method | _generate |
Undocumented |
Method | _compiler |
Undocumented |
Class Variable | _original |
Undocumented |
Construct a new :class:`_expression.Select`. The public constructor for :class:`_expression.Select` is the :func:`_sql.select` function.
def add_columns(self, *entities:
_ColumnsClauseArgument[ Any]
) -> Select[ Any]
:
(source)
¶
Return a new :func:`_expression.select` construct with the given entities appended to its columns clause. E.g.:: my_select = my_select.add_columns(table.c.new_column) The original expressions in the columns clause remain in place. To replace the original expressions with new ones, see the method :meth:`_expression.Select.with_only_columns`. :param \*entities: column, table, or other entity expressions to be added to the columns clause .. seealso:: :meth:`_expression.Select.with_only_columns` - replaces existing expressions rather than appending. :ref:`orm_queryguide_select_multiple_entities` - ORM-centric example
def column(self, column:
_ColumnsClauseArgument[ Any]
) -> Select[ Any]
:
(source)
¶
Return a new :func:`_expression.select` construct with the given column expression added to its columns clause. E.g.:: my_select = my_select.column(table.c.new_column) See the documentation for :meth:`_expression.Select.with_only_columns` for guidelines on adding /replacing the columns of a :class:`_expression.Select` object.
def correlate(self, *fromclauses:
Union[ Literal[ None, False], _FromClauseArgument]
) -> Self
:
(source)
¶
Return a new :class:`_expression.Select` which will correlate the given FROM clauses to that of an enclosing :class:`_expression.Select`. Calling this method turns off the :class:`_expression.Select` object's default behavior of "auto-correlation". Normally, FROM elements which appear in a :class:`_expression.Select` that encloses this one via its :term:`WHERE clause`, ORDER BY, HAVING or :term:`columns clause` will be omitted from this :class:`_expression.Select` object's :term:`FROM clause`. Setting an explicit correlation collection using the :meth:`_expression.Select.correlate` method provides a fixed list of FROM objects that can potentially take place in this process. When :meth:`_expression.Select.correlate` is used to apply specific FROM clauses for correlation, the FROM elements become candidates for correlation regardless of how deeply nested this :class:`_expression.Select` object is, relative to an enclosing :class:`_expression.Select` which refers to the same FROM object. This is in contrast to the behavior of "auto-correlation" which only correlates to an immediate enclosing :class:`_expression.Select`. Multi-level correlation ensures that the link between enclosed and enclosing :class:`_expression.Select` is always via at least one WHERE/ORDER BY/HAVING/columns clause in order for correlation to take place. If ``None`` is passed, the :class:`_expression.Select` object will correlate none of its FROM entries, and all will render unconditionally in the local FROM clause. :param \*fromclauses: one or more :class:`.FromClause` or other FROM-compatible construct such as an ORM mapped entity to become part of the correlate collection; alternatively pass a single value ``None`` to remove all existing correlations. .. seealso:: :meth:`_expression.Select.correlate_except` :ref:`tutorial_scalar_subquery`
def correlate_except(self, *fromclauses:
Union[ Literal[ None, False], _FromClauseArgument]
) -> Self
:
(source)
¶
Return a new :class:`_expression.Select` which will omit the given FROM clauses from the auto-correlation process. Calling :meth:`_expression.Select.correlate_except` turns off the :class:`_expression.Select` object's default behavior of "auto-correlation" for the given FROM elements. An element specified here will unconditionally appear in the FROM list, while all other FROM elements remain subject to normal auto-correlation behaviors. If ``None`` is passed, or no arguments are passed, the :class:`_expression.Select` object will correlate all of its FROM entries. :param \*fromclauses: a list of one or more :class:`_expression.FromClause` constructs, or other compatible constructs (i.e. ORM-mapped classes) to become part of the correlate-exception collection. .. seealso:: :meth:`_expression.Select.correlate` :ref:`tutorial_scalar_subquery`
Return a new :func:`_expression.select` construct which will apply DISTINCT to its columns clause. :param \*expr: optional column expressions. When present, the PostgreSQL dialect will render a ``DISTINCT ON (<expressions>>)`` construct. .. deprecated:: 1.4 Using \*expr in other dialects is deprecated and will raise :class:`_exc.CompileError` in a future version.
Return a SQL ``EXCEPT`` of this select() construct against the given selectable provided as positional arguments. :param \*other: one or more elements with which to create a UNION. .. versionchanged:: 1.4.28 multiple elements are now accepted.
Return a SQL ``EXCEPT ALL`` of this select() construct against the given selectables provided as positional arguments. :param \*other: one or more elements with which to create a UNION. .. versionchanged:: 1.4.28 multiple elements are now accepted.
Apply the columns which this :class:`.Select` would select onto another statement. This operation is :term:`plugin-specific` and will raise a not supported exception if this :class:`_sql.Select` does not select from plugin-enabled entities. The statement is typically either a :func:`_expression.text` or :func:`_expression.select` construct, and should return the set of columns appropriate to the entities represented by this :class:`.Select`. .. seealso:: :ref:`orm_queryguide_selecting_text` - usage examples in the ORM Querying Guide
Return immediate child :class:`.visitors.HasTraverseInternals` elements of this :class:`.visitors.HasTraverseInternals`. This is used for visit traversal. \**kw may contain flags that change the collection that is returned, for example to return a subset of items in order to cut down on larger traversals, or to return child items from a different context (such as schema-level collections instead of clause-level).
Compute the final displayed list of :class:`_expression.FromClause` elements. This method will run through the full computation required to determine what FROM elements will be displayed in the resulting SELECT statement, including shadowing individual tables with JOIN objects, as well as full computation for ORM use cases including eager loading clauses. For ORM use, this accessor returns the **post compilation** list of FROM objects; this collection will include elements such as eagerly loaded tables and joins. The objects will **not** be ORM enabled and not work as a replacement for the :meth:`_sql.Select.select_froms` collection; additionally, the method is not well performing for an ORM enabled statement as it will incur the full ORM construction process. To retrieve the FROM list that's implied by the "columns" collection passed to the :class:`_sql.Select` originally, use the :attr:`_sql.Select.columns_clause_froms` accessor. To select from an alternative set of columns while maintaining the FROM list, use the :meth:`_sql.Select.with_only_columns` method and pass the :paramref:`_sql.Select.with_only_columns.maintain_column_froms` parameter. .. versionadded:: 1.4.23 - the :meth:`_sql.Select.get_final_froms` method replaces the previous :attr:`_sql.Select.froms` accessor, which is deprecated. .. seealso:: :attr:`_sql.Select.columns_clause_froms`
Return a new :func:`_expression.select` construct with the given expression added to its HAVING clause, joined to the existing clause via AND, if any.
Return a SQL ``INTERSECT`` of this select() construct against the given selectables provided as positional arguments. :param \*other: one or more elements with which to create a UNION. .. versionchanged:: 1.4.28 multiple elements are now accepted. :param \**kwargs: keyword arguments are forwarded to the constructor for the newly created :class:`_sql.CompoundSelect` object.
Return a SQL ``INTERSECT ALL`` of this select() construct against the given selectables provided as positional arguments. :param \*other: one or more elements with which to create a UNION. .. versionchanged:: 1.4.28 multiple elements are now accepted. :param \**kwargs: keyword arguments are forwarded to the constructor for the newly created :class:`_sql.CompoundSelect` object.
Return ``True`` if this :class:`.ReturnsRows` is 'derived' from the given :class:`.FromClause`. An example would be an Alias of a Table is derived from that Table.
def join(self, target:
_JoinTargetArgument
, onclause: Optional[ _OnClauseArgument]
= None, *, isouter: bool
= False, full: bool
= False) -> Self
:
(source)
¶
Create a SQL JOIN against this :class:`_expression.Select` object's criterion and apply generatively, returning the newly resulting :class:`_expression.Select`. E.g.:: stmt = select(user_table).join(address_table, user_table.c.id == address_table.c.user_id) The above statement generates SQL similar to:: SELECT user.id, user.name FROM user JOIN address ON user.id = address.user_id .. versionchanged:: 1.4 :meth:`_expression.Select.join` now creates a :class:`_sql.Join` object between a :class:`_sql.FromClause` source that is within the FROM clause of the existing SELECT, and a given target :class:`_sql.FromClause`, and then adds this :class:`_sql.Join` to the FROM clause of the newly generated SELECT statement. This is completely reworked from the behavior in 1.3, which would instead create a subquery of the entire :class:`_expression.Select` and then join that subquery to the target. This is a **backwards incompatible change** as the previous behavior was mostly useless, producing an unnamed subquery rejected by most databases in any case. The new behavior is modeled after that of the very successful :meth:`_orm.Query.join` method in the ORM, in order to support the functionality of :class:`_orm.Query` being available by using a :class:`_sql.Select` object with an :class:`_orm.Session`. See the notes for this change at :ref:`change_select_join`. :param target: target table to join towards :param onclause: ON clause of the join. If omitted, an ON clause is generated automatically based on the :class:`_schema.ForeignKey` linkages between the two tables, if one can be unambiguously determined, otherwise an error is raised. :param isouter: if True, generate LEFT OUTER join. Same as :meth:`_expression.Select.outerjoin`. :param full: if True, generate FULL OUTER join. .. seealso:: :ref:`tutorial_select_join` - in the :doc:`/tutorial/index` :ref:`orm_queryguide_joins` - in the :ref:`queryguide_toplevel` :meth:`_expression.Select.join_from` :meth:`_expression.Select.outerjoin`
def join_from(self, from_:
_FromClauseArgument
, target: _JoinTargetArgument
, onclause: Optional[ _OnClauseArgument]
= None, *, isouter: bool
= False, full: bool
= False) -> Self
:
(source)
¶
Create a SQL JOIN against this :class:`_expression.Select` object's criterion and apply generatively, returning the newly resulting :class:`_expression.Select`. E.g.:: stmt = select(user_table, address_table).join_from( user_table, address_table, user_table.c.id == address_table.c.user_id ) The above statement generates SQL similar to:: SELECT user.id, user.name, address.id, address.email, address.user_id FROM user JOIN address ON user.id = address.user_id .. versionadded:: 1.4 :param from\_: the left side of the join, will be rendered in the FROM clause and is roughly equivalent to using the :meth:`.Select.select_from` method. :param target: target table to join towards :param onclause: ON clause of the join. :param isouter: if True, generate LEFT OUTER join. Same as :meth:`_expression.Select.outerjoin`. :param full: if True, generate FULL OUTER join. .. seealso:: :ref:`tutorial_select_join` - in the :doc:`/tutorial/index` :ref:`orm_queryguide_joins` - in the :ref:`queryguide_toplevel` :meth:`_expression.Select.join`
_JoinTargetArgument
, onclause: Optional[ _OnClauseArgument]
= None, *, full: bool
= False) -> Self
:
(source)
¶
Create a left outer join. Parameters are the same as that of :meth:`_expression.Select.join`. .. versionchanged:: 1.4 :meth:`_expression.Select.outerjoin` now creates a :class:`_sql.Join` object between a :class:`_sql.FromClause` source that is within the FROM clause of the existing SELECT, and a given target :class:`_sql.FromClause`, and then adds this :class:`_sql.Join` to the FROM clause of the newly generated SELECT statement. This is completely reworked from the behavior in 1.3, which would instead create a subquery of the entire :class:`_expression.Select` and then join that subquery to the target. This is a **backwards incompatible change** as the previous behavior was mostly useless, producing an unnamed subquery rejected by most databases in any case. The new behavior is modeled after that of the very successful :meth:`_orm.Query.join` method in the ORM, in order to support the functionality of :class:`_orm.Query` being available by using a :class:`_sql.Select` object with an :class:`_orm.Session`. See the notes for this change at :ref:`change_select_join`. .. seealso:: :ref:`tutorial_select_join` - in the :doc:`/tutorial/index` :ref:`orm_queryguide_joins` - in the :ref:`queryguide_toplevel` :meth:`_expression.Select.join`
_FromClauseArgument
, target: _JoinTargetArgument
, onclause: Optional[ _OnClauseArgument]
= None, *, full: bool
= False) -> Self
:
(source)
¶
Create a SQL LEFT OUTER JOIN against this :class:`_expression.Select` object's criterion and apply generatively, returning the newly resulting :class:`_expression.Select`. Usage is the same as that of :meth:`_selectable.Select.join_from`.
def reduce_columns(self, only_synonyms:
bool
= True) -> Select[ Any]
:
(source)
¶
Return a new :func:`_expression.select` construct with redundantly named, equivalently-valued columns removed from the columns clause. "Redundant" here means two columns where one refers to the other either based on foreign key, or via a simple equality comparison in the WHERE clause of the statement. The primary purpose of this method is to automatically construct a select statement with all uniquely-named columns, without the need to use table-qualified labels as :meth:`_expression.Select.set_label_style` does. When columns are omitted based on foreign key, the referred-to column is the one that's kept. When columns are omitted based on WHERE equivalence, the first column in the columns clause is the one that's kept. :param only_synonyms: when True, limit the removal of columns to those which have the same name as the equivalent. Otherwise, all columns that are equivalent to another are removed.
Return a 'scalar' representation of this selectable, which can be used as a column expression. The returned object is an instance of :class:`_sql.ScalarSelect`. Typically, a select statement which has only one column in its columns clause is eligible to be used as a scalar expression. The scalar subquery can then be used in the WHERE clause or columns clause of an enclosing SELECT. Note that the scalar subquery differentiates from the FROM-level subquery that can be produced using the :meth:`_expression.SelectBase.subquery` method. .. versionchanged: 1.4 - the ``.as_scalar()`` method was renamed to :meth:`_expression.SelectBase.scalar_subquery`. .. seealso:: :ref:`tutorial_scalar_subquery` - in the 2.0 tutorial
Return a new :func:`_expression.select` construct with the given FROM expression(s) merged into its list of FROM objects. E.g.:: table1 = table('t1', column('a')) table2 = table('t2', column('b')) s = select(table1.c.a).\ select_from( table1.join(table2, table1.c.a==table2.c.b) ) The "from" list is a unique set on the identity of each element, so adding an already present :class:`_schema.Table` or other selectable will have no effect. Passing a :class:`_expression.Join` that refers to an already present :class:`_schema.Table` or other selectable will have the effect of concealing the presence of that selectable as an individual element in the rendered FROM list, instead rendering it into a JOIN clause. While the typical purpose of :meth:`_expression.Select.select_from` is to replace the default, derived FROM clause with a join, it can also be called with individual table elements, multiple times if desired, in the case that the FROM clause cannot be fully derived from the columns clause:: select(func.count('*')).select_from(table1)
def selected_columns(self) ->
ColumnCollection[ str, ColumnElement[ Any]]
:
(source)
¶
A :class:`_expression.ColumnCollection` representing the columns that this SELECT statement or similar construct returns in its result set, not including :class:`_sql.TextClause` constructs. This collection differs from the :attr:`_expression.FromClause.columns` collection of a :class:`_expression.FromClause` in that the columns within this collection cannot be directly nested inside another SELECT statement; a subquery must be applied first which provides for the necessary parenthesization required by SQL. For a :func:`_expression.select` construct, the collection here is exactly what would be rendered inside the "SELECT" statement, and the :class:`_expression.ColumnElement` objects are directly present as they were given, e.g.:: col1 = column('q', Integer) col2 = column('p', Integer) stmt = select(col1, col2) Above, ``stmt.selected_columns`` would be a collection that contains the ``col1`` and ``col2`` objects directly. For a statement that is against a :class:`_schema.Table` or other :class:`_expression.FromClause`, the collection will use the :class:`_expression.ColumnElement` objects that are in the :attr:`_expression.FromClause.c` collection of the from element. .. note:: The :attr:`_sql.Select.selected_columns` collection does not include expressions established in the columns clause using the :func:`_sql.text` construct; these are silently omitted from the collection. To use plain textual column expressions inside of a :class:`_sql.Select` construct, use the :func:`_sql.literal_column` construct. .. versionadded:: 1.4
Optional[ OperatorType]
= None) -> Union[ SelectStatementGrouping[ Self], Self]
:
(source)
¶
Apply a 'grouping' to this :class:`_expression.ClauseElement`. This method is overridden by subclasses to return a "grouping" construct, i.e. parenthesis. In particular it's used by "binary" expressions to provide a grouping around themselves when placed into a larger expression, as well as by :func:`_expression.select` constructs when placed into the FROM clause of another :func:`_expression.select`. (Note that subqueries should be normally created using the :meth:`_expression.Select.alias` method, as many platforms require nested SELECT statements to be named). As expressions are composed together, the application of :meth:`self_group` is automatic - end-user code should never need to use this method directly. Note that SQLAlchemy's clause constructs take operator precedence into account - so parenthesis might not be needed, for example, in an expression like ``x OR (y AND z)`` - AND takes precedence over OR. The base :meth:`self_group` method of :class:`_expression.ClauseElement` just returns self.
Return a SQL ``UNION`` of this select() construct against the given selectables provided as positional arguments. :param \*other: one or more elements with which to create a UNION. .. versionchanged:: 1.4.28 multiple elements are now accepted. :param \**kwargs: keyword arguments are forwarded to the constructor for the newly created :class:`_sql.CompoundSelect` object.
Return a SQL ``UNION ALL`` of this select() construct against the given selectables provided as positional arguments. :param \*other: one or more elements with which to create a UNION. .. versionchanged:: 1.4.28 multiple elements are now accepted. :param \**kwargs: keyword arguments are forwarded to the constructor for the newly created :class:`_sql.CompoundSelect` object.
Return a new :func:`_expression.select` construct with the given expression added to its WHERE clause, joined to the existing clause via AND, if any.
@overload
@overload
_TCCA[ _T0]
, __ent1: _TCCA[ _T1]
, __ent2: _TCCA[ _T2]
) -> Select[ Tuple[ _T0, _T1, _T2]]
:_TCCA[ _T0]
, __ent1: _TCCA[ _T1]
, __ent2: _TCCA[ _T2]
, __ent3: _TCCA[ _T3]
) -> Select[ Tuple[ _T0, _T1, _T2, _T3]]
:_TCCA[ _T0]
, __ent1: _TCCA[ _T1]
, __ent2: _TCCA[ _T2]
, __ent3: _TCCA[ _T3]
, __ent4: _TCCA[ _T4]
) -> Select[ Tuple[ _T0, _T1, _T2, _T3, _T4]]
:_TCCA[ _T0]
, __ent1: _TCCA[ _T1]
, __ent2: _TCCA[ _T2]
, __ent3: _TCCA[ _T3]
, __ent4: _TCCA[ _T4]
, __ent5: _TCCA[ _T5]
) -> Select[ Tuple[ _T0, _T1, _T2, _T3, _T4, _T5]]
:_TCCA[ _T0]
, __ent1: _TCCA[ _T1]
, __ent2: _TCCA[ _T2]
, __ent3: _TCCA[ _T3]
, __ent4: _TCCA[ _T4]
, __ent5: _TCCA[ _T5]
, __ent6: _TCCA[ _T6]
) -> Select[ Tuple[ _T0, _T1, _T2, _T3, _T4, _T5, _T6]]
:_TCCA[ _T0]
, __ent1: _TCCA[ _T1]
, __ent2: _TCCA[ _T2]
, __ent3: _TCCA[ _T3]
, __ent4: _TCCA[ _T4]
, __ent5: _TCCA[ _T5]
, __ent6: _TCCA[ _T6]
, __ent7: _TCCA[ _T7]
) -> Select[ Tuple[ _T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7]]
:_ColumnsClauseArgument[ Any]
, maintain_column_froms: bool
= False, **__kw: Any
) -> Select[ Any]
:(source) ¶
Return a new :func:`_expression.select` construct with its columns clause replaced with the given entities. By default, this method is exactly equivalent to as if the original :func:`_expression.select` had been called with the given entities. E.g. a statement:: s = select(table1.c.a, table1.c.b) s = s.with_only_columns(table1.c.b) should be exactly equivalent to:: s = select(table1.c.b) In this mode of operation, :meth:`_sql.Select.with_only_columns` will also dynamically alter the FROM clause of the statement if it is not explicitly stated. To maintain the existing set of FROMs including those implied by the current columns clause, add the :paramref:`_sql.Select.with_only_columns.maintain_column_froms` parameter:: s = select(table1.c.a, table2.c.b) s = s.with_only_columns(table1.c.a, maintain_column_froms=True) The above parameter performs a transfer of the effective FROMs in the columns collection to the :meth:`_sql.Select.select_from` method, as though the following were invoked:: s = select(table1.c.a, table2.c.b) s = s.select_from(table1, table2).with_only_columns(table1.c.a) The :paramref:`_sql.Select.with_only_columns.maintain_column_froms` parameter makes use of the :attr:`_sql.Select.columns_clause_froms` collection and performs an operation equivalent to the following:: s = select(table1.c.a, table2.c.b) s = s.select_from(*s.columns_clause_froms).with_only_columns(table1.c.a) :param \*entities: column expressions to be used. :param maintain_column_froms: boolean parameter that will ensure the FROM list implied from the current columns clause will be transferred to the :meth:`_sql.Select.select_from` method first. .. versionadded:: 1.4.23
Return a :term:`plugin-enabled` 'column descriptions' structure referring to the columns which are SELECTed by this statement. This attribute is generally useful when using the ORM, as an extended structure which includes information about mapped entities is returned. The section :ref:`queryguide_inspection` contains more background. For a Core-only statement, the structure returned by this accessor is derived from the same objects that are returned by the :attr:`.Select.selected_columns` accessor, formatted as a list of dictionaries which contain the keys ``name``, ``type`` and ``expr``, which indicate the column expressions to be selected:: >>> stmt = select(user_table) >>> stmt.column_descriptions [ { 'name': 'id', 'type': Integer(), 'expr': Column('id', Integer(), ...)}, { 'name': 'name', 'type': String(length=30), 'expr': Column('name', String(length=30), ...)} ] .. versionchanged:: 1.4.33 The :attr:`.Select.column_descriptions` attribute returns a structure for a Core-only set of entities, not just ORM-only entities. .. seealso:: :attr:`.UpdateBase.entity_description` - entity information for an :func:`.insert`, :func:`.update`, or :func:`.delete` :ref:`queryguide_inspection` - ORM background
Return the set of :class:`_expression.FromClause` objects implied by the columns clause of this SELECT statement. .. versionadded:: 1.4.23 .. seealso:: :attr:`_sql.Select.froms` - "final" FROM list taking the full statement into account :meth:`_sql.Select.with_only_columns` - makes use of this collection to set up a new FROM list
@util.deprecated(
froms:
Sequence[ FromClause]
=
(source)
¶
Return the displayed list of :class:`_expression.FromClause` elements.
An iterator of all :class:`_expression.ColumnElement` expressions which would be rendered into the columns clause of the resulting SELECT statement. This method is legacy as of 1.4 and is superseded by the :attr:`_expression.Select.exported_columns` collection.
Return the completed WHERE clause for this :class:`_expression.Select` statement. This assembles the current collection of WHERE criteria into a single :class:`_expression.BooleanClauseList` construct. .. versionadded:: 1.4
Create a :class:`.Select` using raw ``__new__`` with no coercions. Used internally to build up :class:`.Select` constructs with pre-established state.
A sequence of expressions that correspond to what is rendered in the columns clause, including :class:`_sql.TextClause` constructs. .. versionadded:: 1.4.12 .. seealso:: :attr:`_sql.SelectBase.exported_columns`
Reassign internal elements to be clones of themselves. Called during a copy-and-traverse operation on newly shallow-copied elements to create a deep copy. The given clone function should be used, which may be applying additional transformations to the element (i.e. replacement traversal, cloned traversal, annotations).
Union[ FromClause, _JoinTargetProtocol, ColumnElement[ Any], TextClause]
:
(source)
¶
Undocumented
FromClause
, *, proxy_compound_columns: Optional[ Iterable[ Sequence[ ColumnElement[ Any]]]]
= None):
(source)
¶
Generate column proxies to place in the exported ``.c`` collection of a subquery.