在sqlalchemy中,我如何查询表格,过滤列的翻译?

时间:2015-11-06 19:46:48

标签: python sqlalchemy

有时搜索值包含随机连字符,有时列数据包含随机连字符,因此我想要忽略所有连字符进行搜索。在搜索词上使用通配符只能在一个方向上起作用(不要忽略表中的连字符)。我如何使用sqlalchemy执行此SQL?

select item_name, replace(item_name,'-','') as new_name 
from items 
where replace(item_name,'-','') like 'ABC%'

1 个答案:

答案 0 :(得分:2)

您可以使用func生成SQL函数表达式

from sqlalchemy import func

session.query(Item.item_name, func.replace(Item.item_name, '-', '').label('new_name')).filter(func.replace(Item.item_name, '-', '').like('ABC%')).all()