Python 在 sqlalchemy 中选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3576382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select as in sqlalchemy
提问by user401574
I want to do something like this:
我想做这样的事情:
select username, userid, 'user' as new_column from users_table.
The columns of the table can be selected using sqlalchemy as follows:
可以使用 sqlalchemy 选择表的列,如下所示:
query = select([users_table.c.username, users_table.c.userid])
How do I do the select xas col_xto the query in sqlalchemy?
我怎么也选择x为col_x在SQLAlchemy的查询?
回答by Radomir Dopieralski
See the alias()in here: http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/expressions.html
请参阅alias()此处:http: //www.sqlalchemy.org/docs/05/reference/sqlalchemy/expressions.html
回答by Mariano
Maybe literal_column?
也许literal_column?
query = select([users_table.c.username, users_table.c.userid, literal_column("user", type_=Unicode).label('new_column')])
See https://docs.sqlalchemy.org/en/13/core/sqlelement.html#sqlalchemy.sql.expression.literal_column
请参阅https://docs.sqlalchemy.org/en/13/core/sqlelement.html#sqlalchemy.sql.expression.literal_column
Edit: actually I should have said "literal":
编辑:实际上我应该说“文字”:
query = select([users_table.c.username, users_table.c.userid, literal("user", type_=Unicode).label('new_column')])
回答by pylover
use this: users_table.c.userid.label('NewColumn')
用这个: users_table.c.userid.label('NewColumn')
i.e.,
IE,
query = select([users_table.c.username, users_table.c.userid.label('NewColumn')])
evaluates to:
评估为:
SELECT username , userid as NewColumn From MyTable;

