[英]Performing union with three queries - SQLAlchemy
In my project setup querying is being done based on the SQLAlchemy.在我的项目设置中,查询是基于 SQLAlchemy 完成的。
As per my previous requirements I have done the union with two queries.根据我之前的要求,我已经完成了两个查询的联合。
Now I need to do Union with three queries.现在我需要用三个查询来联合。
Code is as follows:代码如下:
query1 = query1.filter(model.name == "in-addr.arpa.")
query2 = query2.filter(model.tenant_id.in_(tenant_ids))
query = query1.union(query2)
Now Here I need to add one more query as follows:现在在这里我需要再添加一个查询,如下所示:
query3 = query3.filter(model.tenant_id == context.tenant_id)
So I need to perform Union with all the three queries.所以我需要对所有三个查询执行联合。
The solution is following:解决方法如下:
query1 = query1.filter(model.name == "in-addr.arpa.")
query2 = query2.filter(model.tenant_id.in_(tenant_ids))
query3 = query3.filter(model.tenant_id == context.tenant_id)
query = query1.union(query2,query3)
This is how I did this in SQLAlchemy 1.3这就是我在 SQLAlchemy 1.3 中这样做的方式
from sqlalchemy import union
query1 = query1.filter(model.name == "in-addr.arpa.")
query2 = query2.filter(model.tenant_id.in_(tenant_ids))
query3 = query3.filter(model.tenant_id == context.tenant_id)
all_queries = [query1, query2, query3]
golden_set = union(*all_queries)
The change here is that the union method accepts a list of SQLAlchemy selectables.此处的更改是 union 方法接受 SQLAlchemy 可选列表。
In SQLAlchemy 1.4 you will need to use the function union and pass the queries as positional arguments instead of a list.在 SQLAlchemy 1.4 中,您将需要使用函数 union 并将查询作为位置参数而不是列表传递。
from sqlalchemy import union
query1 = query1.filter(model.name == "in-addr.arpa.")
query2 = query2.filter(model.tenant_id.in_(tenant_ids))
query3 = query3.filter(model.tenant_id == context.tenant_id)
query = union(query1, query2, query3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.