diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-07-05 15:55:20 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2019-07-05 15:56:48 -0400 |
commit | 71a13da0228330c82f4ec226fe5ba39fc78074d3 (patch) | |
tree | f2d0ca8ac418ec3f960deeead02c0cc543f4dad5 | |
parent | 5acf1ac51e9b7b5270c2f21838a5df0ed408e4e8 (diff) | |
download | sqlalchemy-71a13da0228330c82f4ec226fe5ba39fc78074d3.tar.gz |
Rename tutorial section to "Using Aliases and Subqueries"
add some verbiage to start differentiating a subquery from
an alias.
Also, get rid of a very strange note to use ``.correlate(None)``
on a non-scalar subquery; this is unnecessary and confusing.
Change-Id: I83b2fd1275c719a32bb74060756d61bc51b52892
(cherry picked from commit f4f9ec1c2f6fad29729ee379adb537f8648d1737)
-rw-r--r-- | doc/build/core/tutorial.rst | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/doc/build/core/tutorial.rst b/doc/build/core/tutorial.rst index c754db816..ef3a71f23 100644 --- a/doc/build/core/tutorial.rst +++ b/doc/build/core/tutorial.rst @@ -1076,8 +1076,8 @@ by a column name that appears more than once: -Using Aliases -============= +Using Aliases and Subqueries +============================ The alias in SQL corresponds to a "renamed" version of a table or SELECT statement, which occurs anytime you say "SELECT .. FROM sometable AS @@ -1088,10 +1088,11 @@ FROM clause multiple times. In the case of a SELECT statement, it provides a parent name for the columns represented by the statement, allowing them to be referenced relative to this name. -In SQLAlchemy, any :class:`.Table`, :func:`.select` construct, or -other selectable can be turned into an alias using the :meth:`.FromClause.alias` -method, which produces a :class:`.Alias` construct. As an example, suppose we know that our user ``jack`` has two -particular email addresses. How can we locate jack based on the combination of those two +In SQLAlchemy, any :class:`.Table`, :func:`.select` construct, or other +selectable can be turned into an alias or named subquery using the +:meth:`.FromClause.alias` method, which produces a :class:`.Alias` construct. +As an example, suppose we know that our user ``jack`` has two particular email +addresses. How can we locate jack based on the combination of those two addresses? To accomplish this, we'd use a join to the ``addresses`` table, once for each address. We create two :class:`.Alias` constructs against ``addresses``, and then use them both within a :func:`.select` construct: @@ -1133,15 +1134,16 @@ to the :meth:`.FromClause.alias` method:: >>> a1 = addresses.alias('a1') Aliases can of course be used for anything which you can SELECT from, -including SELECT statements themselves. We can self-join the ``users`` table +including SELECT statements themselves, by converting the SELECT statement +into a named subquery. The :meth:`.SelectBase.alias` method performs this +role. We can self-join the ``users`` table back to the :func:`.select` we've created by making an alias of the entire -statement. The ``correlate(None)`` directive is to avoid SQLAlchemy's attempt -to "correlate" the inner ``users`` table with the outer one: +statement: .. sourcecode:: pycon+sql - >>> a1 = s.correlate(None).alias() - >>> s = select([users.c.name]).where(users.c.id == a1.c.id) + >>> addresses_subq = s.alias() + >>> s = select([users.c.name]).where(users.c.id == addresses_subq.c.id) {sql}>>> conn.execute(s).fetchall() SELECT users.name FROM users, |