You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/dart_api/select.md
+31-5Lines changed: 31 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,9 +5,6 @@ description: Select rows or individual columns from tables in Dart
5
5
6
6
---
7
7
8
-
9
-
10
-
11
8
This page describes how to write `SELECT` statements with drift's dart_api.
12
9
To make examples easier to grasp, they're referencing two common tables forming
13
10
the basis of a todo-list app:
@@ -288,8 +285,6 @@ Any statement can be used as a subquery. But be aware that, unlike [subquery exp
288
285
289
286
## JSON support
290
287
291
-
292
-
293
288
sqlite3 has great support for [JSON operators](https://sqlite.org/json1.html) that are also available
294
289
in drift (under the additional `'package:drift/extensions/json1.dart'` import).
295
290
JSON support is helpful when storing a dynamic structure that is best represented with JSON, or when
@@ -333,3 +328,34 @@ at all.
333
328
Instead, the expressions in the list passed to `selectExpressions` are evaluated in a standalone
334
329
select statement and can be parsed from the `TypedResult` class returned when evaluating the
335
330
query.
331
+
332
+
## Compound selects
333
+
334
+
With compound selects, the results of multiple selects statements can be returned at once.
335
+
Different operators are available to apply set operations on queries, namely:
336
+
337
+
1.`UNION ALL` and `UNION`: Returns the results of two select statements in a select, with
338
+
duplicates included or filtered, respectively.
339
+
2.`EXCEPT`: Returns all rows of the first select statement that did not appear in the second
340
+
query.
341
+
3.`INTERSECT`: Returns all rows that were returned by both select statements.
342
+
343
+
As an example, consider the tables used to track todo items introduced [in the article on tables](tables.md#defining-tables). Here, one table stores todo items and another table defines categories
344
+
that can be used to group these items.
345
+
Now, perhaps you want to query how many items are assigned to each category, as well as the amount
346
+
of items not in any category.
347
+
The first query can be written with a `groupBy` on categories and a [subquery](expressions.md#subqueries)
348
+
to count associated todo items.
349
+
When grouping on the categories table though, there will be no "null" group. So, one way to resolve
350
+
everything in a single query is to write another query and use `unionAll`:
0 commit comments