在sqlalchemy中查询时是否有可能组合两列。
ex,有下表。
table 1 table 2
col1 col2 col3 col1 col2 col3
A 123 1 ABC A1B2
B 456 2 DEF C3D4
C 789 3 GHI E5F6
我的查询将是这样的:
session.query(table.col3, (table1.col2 + table2.col2)).join(table2)
并且应该给我以下结果:
(1, 123A1B2)
(2, 456C3D4)
(3, 789E5F6)
我将如何做到这一点?
答案 0 :(得分:6)
它当然取决于数据库和类型;如果table1.col2
是一个数字,那么您可能想尝试:
from sqlalchemy.sql import expression, functions
from sqlalchemy import types
... functions.concat(
expressions.cast(table1.col2, types.Unicode), table2.col2) ...
通常,+运算符应该适用于2个文本列。