Python SQLAlchemy与或匹配
我正被一些我正在尝试的炼金术所束缚。我有一个旧的网络应用程序,我正试图启动,并已决定重写它从头开始。作为其中的一部分,我正在玩SQL炼金术,并试图提高我的Python技能-因此我有一个搜索对象,我正在尝试运行,在其中我检查客户查询是否存在于account name和customer name字段中,并与之匹配。然而,SQL Alchemy将其注册为AND 如果我添加额外的或uu块,它将无法识别它们并进行适当的处理 我移动了它,所以它是第一个查询,但是sqlalchemy中的查询计划器使它完全相同 有什么想法吗Python SQLAlchemy与或匹配,python,flask,sqlalchemy,Python,Flask,Sqlalchemy,我正被一些我正在尝试的炼金术所束缚。我有一个旧的网络应用程序,我正试图启动,并已决定重写它从头开始。作为其中的一部分,我正在玩SQL炼金术,并试图提高我的Python技能-因此我有一个搜索对象,我正在尝试运行,在其中我检查客户查询是否存在于account name和customer name字段中,并与之匹配。然而,SQL Alchemy将其注册为AND 如果我添加额外的或uu块,它将无法识别它们并进行适当的处理 我移动了它,所以它是第一个查询,但是sqlalchemy中的查询计划器使它完全相同
def CustomerCountryMatch(query, page):
customer=models.Customer
country=models.CustomerCodes
query=customer.query.order_by(customer.account_name).\
group_by(customer.account_name).having(func.max(customer.renewal_date)).\
join(country, customer.country_code==country.CODE).\
add_columns(customer.account_name,
customer.customer_name,
customer.account_id,
customer.CustomerNote,
country.COUNTRY,
country.SupportRegion,
customer.renewal_date,
customer.contract_type,
customer.CCGroup).\
filter(customer.account_name.match(query)).filter(or_(customer.customer_name.match(query))).\
paginate(page, 50, False)
执行的查询如下所示:
sqlalchemy.engine.base.engine选择customer.customer\u id作为customer\u customer\u id,
customer.customer\u代码作为customer\u customer\u代码,
customer.address\u代码作为customer\u地址\u代码,
customer.customer\u name作为customer\u customer\u name,
customer.account\u id作为customer\u account\u id,
customer.account\u name作为customer\u account\u name,
客户。`CustomerNote`作为`customer\u CustomerNote`,
customer.renewal\u date作为customer\u renewal\u date,
customer.contract\u type作为customer\u contract\u type,
customer.country\u代码作为customer\u country\u代码,
客户。`CCGroup`作为`customer\CCGroup`,
客户。`AgentStatus`作为`customer\u AgentStatus`,
customer.comments作为customer\u注释,
客户。`SCR`作为`customer\u SCR`,
customer.`isDummy`作为`customer\u isDummy`,
客户代码。`COUNTRY`作为`customer\u code\u COUNTRY`,
客户代码。`SupportRegion`作为`customer\u代码`SupportRegion`
从客户内部加入
客户上的客户代码。国家代码=客户代码。`code`WHERE
与(%s处于布尔模式)匹配(customer.account\u name)并
将(customer.customer_name)与分组依据(%s处于布尔模式)匹配
具有最大订单日期(客户续订日期)的customer.account\u名称
customer.account\u名称限制%s,
%s 2015-11-06 03:32:52035信息sqlalchemy.engine.base.engine('bob','bob',0,50)
筛选子句应为:
filter(
or_(
customer.account_name.match(query),
customer.customer_name.match(query)
)
)
调用filter
两次,如filter(第1条)。filter(第2条)
使用和连接条件(请参阅)
构造:filter(第1条)。filter(或(第2条))
不符合您的要求,它被转换为SQL:clause1和clause2
下面的示例很有意义:filter(第1条)。filter(或(第2条,第3条))
,并将其翻译为SQL:第1条和(第2条或第3条)
filter子句应为:
filter(
or_(
customer.account_name.match(query),
customer.customer_name.match(query)
)
)
调用filter
两次,如filter(第1条)。filter(第2条)
使用和连接条件(请参阅)
构造:filter(第1条)。filter(或(第2条))
不符合您的要求,它被转换为SQL:clause1和clause2
下面的示例很有意义:filter(第1条)。filter(或(第2条,第3条))
,并被翻译成SQL:第1条和(第2条或第3条)
如果要查找包含正在搜索的一个或多个单词的所有匹配项,则更简单的方法是在匹配项中使用OR子句并使用“|”运算符,例如
query = query.filter(Table.text_searchable_column.match('findme | orme'))
一种更简单的方法是,如果要查找包含正在搜索的一个或多个单词的所有匹配项,请在匹配项中使用OR子句并使用“|”运算符,例如
query = query.filter(Table.text_searchable_column.match('findme | orme'))
谢谢@codeape-我不知道你可以使用这种语法。很多感谢感谢@codeape-我不知道你可以使用这种语法。非常感谢