简体   繁体   English

在 SQLAlchemy (Python) 中联合查询列表的正确方法是什么?

[英]What's the proper way to UNION a list of queries in SQLAlchemy (Python)?

So I have a list of SQLAlchemy Query objects and I want to UNION all of them.所以我有一个 SQLAlchemy Query对象的列表,我想 UNION 所有这些对象。

For example if I have in my list the equivalent of:例如,如果我的列表中有以下等价物:

SELECT id, name FROM person
SELECT id, name FROM employee

I would get the equivalent of:我会得到相当于:

(SELECT id, name FROM person)
UNION
(SELECT id, name FROM employee)

I don't know what's in the list and the list could contain many Query objects but of course the result signature of each SQLAlchemy Query object in my list is 100% identical.我不知道列表中有什么,列表可能包含许多Query对象,但当然我列表中每个 SQLAlchemy Query object 的结果签名是 100% 相同的。

At the moment I do the UNION like this:目前我这样做 UNION 是这样的:

if not q_list:
    return []

big_q = q_list.pop(0)
if q_list:
    big_q = big_q.union(*q_list)
result = [dict(row) for row in dbsession.execute(big_q)]

But for some weird reason I don't get the same result as if I were running all the queries individually then concatenating the results together.但是由于某些奇怪的原因,我没有得到与我单独运行所有查询然后将结果连接在一起时相同的结果。 I must do the union incorrectly.我必须错误地做联合。 How do I do the UNION then?那我该怎么做 UNION 呢?

I think you might be expecting UNION ALL .我想你可能期待UNION ALL

From the postgresql documentation , the second part..来自postgresql 文档,第二部分..

UNION effectively appends the result of query2 to the result of query1 (although there is no guarantee that this is the order in which the rows are actually returned). UNION 有效地将 query2 的结果附加到 query1 的结果(尽管不能保证这是实际返回行的顺序)。

Furthermore, it eliminates duplicate rows from its result, in the same way as DISTINCT, unless UNION ALL is used.此外,它以与 DISTINCT 相同的方式从其结果中消除重复行,除非使用 UNION ALL。

Try this:尝试这个:

if q_list:
    big_q = big_q.union_all(*q_list)

There is also a function to perform union all , like:还有一个 function 来执行union all ,例如:

q = union_all(*q_list)

My problem had nothing to do with the union call.我的问题与union号召无关。 My queries list to UNION was created with a function which was doing something like this on each object:我对 UNION 的查询列表是用 function 创建的,它在每个 object 上做这样的事情:

sql.expression.bindparam(
    "current_timestamp", value=start_timestamp
)

It seems that since all my query object in my list had the same params names ( current_timestamp ) they all used the same value instead of their individual value when bindparam was used.似乎因为我列表中的所有查询 object 都具有相同的参数名称 ( current_timestamp ),所以当使用bindparam时,它们都使用相同的值而不是它们各自的值。

I just had to use the unique parameter to fix my problem:我只需要使用unique参数来解决我的问题:

sql.expression.bindparam(
    "current_timestamp", unique=True, value=start_timestamp
)

That way they all get their start_timestamp value as expected.这样他们都能按预期获得start_timestamp值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLalchemy以声明方式描述关联对象的正确方法是什么 - What's the proper way to describe an associative object by SQLalchemy the declarative way 在 Flask SQLALchemy 模型中处理业务逻辑的正确方法是什么? - What's the proper way of handling business logic in Flask SQLALchemy models? 将 SQLAlchemy 会话与 Celery 一起使用的正确方法是什么? - What's the proper way to use SQLAlchemy Sessions with Celery? 使用SQLAlchemy为这种继承建模的正确方法是什么? - What is the proper way to model this type of inheritance with SQLAlchemy? 在正则表达式 python 中排除大写单词的正确方法是什么 - What's the proper way to exclude uppercase word/s in regex python 使用三个查询执行联合 - SQLAlchemy - Performing union with three queries - SQLAlchemy 在List子类的方法中对“ self”进行切片的正确方法是什么? - What's the proper way to slice `self` within a method of a List subclass? 在Python中打破嵌套函数/构造函数调用的正确方法是什么? - What's the proper way to break nested function/constructor calls in Python? 在Python中编写游戏循环的正确方法是什么? - What's the proper way to write a game loop in Python? 使Python 3函数接受预设的正确方法是什么? - What's the proper way to make a Python 3 function accept presets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM