DB Count
The DB Count block counts the records in a table that match your conditions and passes on that number. Use it for "how many active orders does this rider have?", or to show a total next to a paginated list, without loading every row.
When to use it
- Show a total next to a list, like "128 results".
- Check a limit before you allow something, like "fewer than 5 open tickets".
- Don't use it just to know whether something exists. DB Row Exists is built for that.
- Don't use it when you need the records too. DB Get All gives you the list, and its length.
Inputs
The same fields as DB Get Single, without Columns.
| Field | Required | Default | What it does |
|---|---|---|---|
| Connection | Yes | none | The database integration to use. |
| Table Name | Yes | none | The table (or collection) to count in. |
| Conditions | No | none (every record) | Rules that pick the records to count, see DB Conditions. A condition whose value is undefined is skipped. |
| Joins | No | none | Other tables to combine, SQL only. See DB Joins. |
| Save output to variable | No | off | Store the number in outputs.<name>. |
Outputs
| Handle | When it is used |
|---|---|
| Next (right) | The count worked. The next block receives a number, for example 3. It is 0 when nothing matches. |
Example
How many active orders does rider 7 have?
| Field | Value |
|---|---|
| Connection | main-db |
| Table Name | orders |
| Conditions | rider_id = 7 AND status = active |
The next block receives 3. Use it in a Response body or an If Condition.
How it behaves
- No conditions counts every record in the table.
- With joins, each joined row counts once. A user with two orders counts twice when you join
orders. - MongoDB: joins are skipped. To combine collections, use a DB Native block.
- A missing table: on MongoDB, a collection that doesn't exist counts as
0. On PostgreSQL and MySQL, a missing table is an error. - Database errors (bad connection, missing table or column) go to the Error Handler.
- It replaces the flowing data with the number.
Related blocks
- DB Get All: load the records too.
- DB Row Exists: only check whether any record exists.
- DB Conditions and DB Joins: the filter and join options in detail.
