我有两个使用SQLAlchemy通过反射生成的SQLAlchemy表:
In [356]: t1
Out[356]: Table('t1', MetaData(bind=None), Column('x', INT(), table=<t1>), Column('y', STRING(), table=<t1>), schema=None)
In [357]: t2
Out[357]: Table('t2', MetaData(bind=None), Column('x', INT(), table=<t2>), Column('y', STRING(), table=<t2>), schema=None)
我想使用上述两个表进行union_all
操作,然后过滤结果以仅在x > 2
时保留。这是我所做的:
q1 = select([t1.c.x.label('x'), t1.c.y.label('y')])
q2 = select([t2.c.x.label('x'), t2.c.y.label('y')])
q3 = q1.union_all(q2)
q = select([q3.c.x, q3.c.y]).where(q3.c.x > 2)
conn.execute(q).fetchall()
但是我得到了
DBAPIError: (impala.error.HiveServer2Error) AnalysisException: Syntax error in line 5:
WHERE x > 2
^
Encountered: WHERE
Expected: AS, DEFAULT, IDENTIFIER
CAUSED BY: Exception: Syntax error
[SQL: 'SELECT x, y \nFROM (SELECT t1.x AS x, t1.y AS y \nFROM t1 UNION ALL SELECT t2.x AS x, t2.y AS y \nFROM t2) \nWHERE x > %(x_1)s'] [parameters: {'x_1': 2}] (Background on this error at: http://sqlalche.me/e/dbapi)
答案 0 :(得分:0)
您的派生表没有名称,或者换句话说,联合表达式在FROM子句中缺少别名。试试:
req.user
这将添加所需的AS。