# Union of multiple SQL queries

**URL:** https://discuss.tryton.org/t/union-of-multiple-sql-queries/2299
**Category:** Developer
**Tags:** python-sql
**Created:** [February 6, 2020, 6:21pm UTC](https://discuss.tryton.org/t/union-of-multiple-sql-queries/2299 "2020-02-06T18:21:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![bell](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@bell](https://discuss.tryton.org/u/bell)
#### Post date: [February 6, 2020, 6:21pm UTC](https://discuss.tryton.org/t/union-of-multiple-sql-queries/2299/1 "2020-02-06T18:21:52Z")

</div>

Suppose we have a union of three queries:

```
union = query1 | query2 | query3
union.all_ = True

```

Why does the ALL key only apply to the union of `query2` and `query3`?

We need the ALL key to be applied to all union operations.

```
union = query1 | query2
union.all_ = True
union |= query3
union.all_ = True

```

Is it correct?

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [February 6, 2020, 11:32pm UTC](https://discuss.tryton.org/t/union-of-multiple-sql-queries/2299/2 "2020-02-06T23:32:25Z")

</div>

> [@bell](#):
>
> Why does the ALL key only apply to the union of `query2` and `query3` ?

Because ` __or__ ` operator is binary so you create a first union which is union-ed again with the last one. This is the last union that you get.

> [@bell](#):
>
> Is it correct?

Well you can just write:

```python
union = Union(query1, query2, query3, all_=True)

```

---

<div class="post-metadata">

### Author: ![bell](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/b/c67d28/32.png) [@bell](https://discuss.tryton.org/u/bell)
#### Post date: [February 7, 2020, 12:17pm UTC](https://discuss.tryton.org/t/union-of-multiple-sql-queries/2299/3 "2020-02-07T12:17:33Z")

</div>

> [@ced](#):
>
> Well you can just write:
> 
> ```auto
> union = Union(query1, query2, query3, all_=True)
> 
> ```

Thank you for the tip.

---

<div class="post-metadata">

### Author: ![system](https://discuss-cdn.tryton.org/uploads/default/original/1X/c6f8ec0a40525cdcd50058c734283450a4b3d38b.png) [@system](https://discuss.tryton.org/u/system)
#### Post date: [March 8, 2020, 12:17pm UTC](https://discuss.tryton.org/t/union-of-multiple-sql-queries/2299/4 "2020-03-08T12:17:37Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
