Python SQLAlchemy别名不是别名?
我有以下sqlalchemy代码:Python SQLAlchemy别名不是别名?,python,sql,database,sqlalchemy,Python,Sql,Database,Sqlalchemy,我有以下sqlalchemy代码: x = bbc.alias().c w = bbc.alias().c select([func.distinct(x.region)]).where( select([func.sum(w.population)]).where((w.region == x.region)) > 100000000 ) 我希望它能产生以下SQL: SELECT DISTINCT(x.region) FROM bbc x WHERE 100000000 <
x = bbc.alias().c
w = bbc.alias().c
select([func.distinct(x.region)]).where(
select([func.sum(w.population)]).where((w.region == x.region)) > 100000000
)
我希望它能产生以下SQL:
SELECT DISTINCT(x.region)
FROM bbc x
WHERE 100000000 < (
SELECT SUM(w.population)
from bbc w
WHERE w.region = x.region
)
不知何故,WHERE条款正在崩溃,但就我而言,我不明白为什么。别名有问题吗?如邮件列表中所述,
select()
构造是一个fromsclause
,直到您在其上调用as\u scalar()
,将其变成适合SQL表达式的ColumnElement
:
x = bbc.alias().c
w = bbc.alias().c
select([func.distinct(x.region)]).where(
select([func.sum(w.population)]).where(w.region == x.region).as_scalar() > 100000000
)
x = bbc.alias().c
w = bbc.alias().c
select([func.distinct(x.region)]).where(
select([func.sum(w.population)]).where(w.region == x.region).as_scalar() > 100000000
)