Next, we encounter that they'd like the results in descending order by full name. We apply ORDER BY, using an extra modifier `desc`:
{python}
- >>> from sqlalchemy.sql import desc
- >>> query = query.order_by(desc(users.c.fullname))
+ >>> query = query.order_by(users.c.fullname.desc())
We also come across that they'd like only users who have an address at MSN. A quick way to tack this on is by using an EXISTS clause, which we correlate to the `users` table in the enclosing SELECT:
{stop}[(1, 2), (2, 2)]
>>> s = select([addresses.c.email_address, addresses.c.id]).distinct().\
- ... order_by(desc(addresses.c.email_address), addresses.c.id)
+ ... order_by(addresses.c.email_address.desc(), addresses.c.id)
{opensql}>>> conn.execute(s).fetchall()
SELECT DISTINCT addresses.email_address, addresses.id
FROM addresses ORDER BY addresses.email_address DESC, addresses.id
def label(self, name):
"""produce a column label, i.e. ``<columnname> AS <name>``"""
return _Label(name, self, self.type)
-
+
+ def desc(self):
+ """produce a DESC clause, i.e. ``<columnname> DESC``"""
+
+ return desc(self)
+
+ def asc(self):
+ """produce a ASC clause, i.e. ``<columnname> ASC``"""
+
+ return asc(self)
+
def distinct(self):
"""produce a DISTINCT clause, i.e. ``DISTINCT <columnname>``"""
return _UnaryExpression(self, operator="DISTINCT")
table2.select(order_by = [table2.c.otherid, asc(table2.c.othername)]),
"SELECT myothertable.otherid, myothertable.othername FROM myothertable ORDER BY myothertable.otherid, myothertable.othername ASC"
)
+
+ self.runtest(
+ table2.select(order_by = [table2.c.otherid, table2.c.othername.desc()]),
+ "SELECT myothertable.otherid, myothertable.othername FROM myothertable ORDER BY myothertable.otherid, myothertable.othername DESC"
+ )
+
def testgroupby(self):
self.runtest(
select([table2.c.othername, func.count(table2.c.otherid)], group_by = [table2.c.othername]),