[英]SQLAlchemy expression language: how to join table with subquery?
I have a subquery table inner_stmt
, which I want to join with a table revisions
. 我有一个子查询表
inner_stmt
,我想将其与表revisions
一起加入。 But revisions.join()
gives the following error: 但是
revisions.join()
给出以下错误:
Neither 'Label' object nor 'Comparator' object has an attribute 'c'
Here is my code. 这是我的代码。 What am I doing wrong?
我究竟做错了什么?
inner_stmt = select([
ratings.c.article_name,
func.min(ratings.c.timestamp).label('mintime')]) \
.group_by(ratings.c.article_name).label('firstga')
stmt = select([
revisions.c.article_id,
func.max(revisions.c.length_bytes)]) \
.select_from(revisions.join(
inner_stmt,
revisions.c.article_name == inner_stmt.c.article_name)) \
.group_by(table.c.article_id)
You are label
ing your subquery inner_stmt
. 您正在
label
子查询inner_stmt
。 label
is for columns or expressions, ie SELECT ... AS ...
. label
用于列或表达式,即SELECT ... AS ...
You want alias
instead, which is for subquery expressions, ie FROM ... AS ...
. 您需要
alias
,该alias
用于子查询表达式,即FROM ... AS ...
You cannot access columns ( .c.<name>
) from the former, even in SQL, because it's a SQL expression. 您无法访问前者中的列(
.c.<name>
),即使在SQL中也是如此,因为它是SQL表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.