LogoLogo
8.5.0
8.5.0
  • Introduction
  • What's New?
  • Installation & Usage
  • Migration Guide
  • Contributing & Filing Issues
  • Query Builder
    • Getting a New Query
    • Building Queries
      • Selects
      • From
      • Joins
      • Wheres
      • Order By
      • Group By and Having
      • Limit, Offset, and Pagination
      • Locks
      • Unions
      • Common Table Expressions (i.e. CTEs)
      • Raw Expressions
      • When / Conditionals
      • Query Parameters and Bindings
    • Executing Queries
      • Retrieving Results
      • Aggregates
      • Inserts, Updates, and Deletes
    • Options and Utilities
      • Query Options
      • Clone and Reset
      • Return Format
      • Column Formatter
      • Parent Query
      • Interception Points
    • Debugging
  • Schema Builder
    • Overview
    • Create
    • Columns
    • Column Modifiers
    • Column Constraints
    • Creating Table Constraints
    • Alter
    • Drop
  • External Links
    • API Docs
    • Source Code
    • Issue Tracker
Powered by GitBook
On this page
  • exists
  • count
  • max
  • min
  • sum

Was this helpful?

Edit on Git
Export as PDF
  1. Query Builder
  2. Executing Queries

Aggregates

The query builder also provides a variety of aggregate methods such as count, max, min, and sum. These methods take the headache out of setting up these common aggregate functions.

When executing any of the aggregate functions, any where restrictions on your query will still be applied.

Instead of returning a query, these methods return a simple value.

exists

Name

Type

Required

Default

Description

options

struct

false

{}

Any additional queryExecute options.

Returns true if the query returns any rows. Returns false otherwise.

QueryBuilder
query.from( "users" ).where( "username", "like", "jon%" ).exists();
SQL (MySQL)
SELECT COUNT(*) AS aggregate 

FROM `users` WHERE `username` LIKE 'jon%'

count

Name

Type

Required

Default

Description

column

string

false

"*"

The column on which to count records.

options

struct

false

{}

Any additional queryExecute options.

Returns an integer number of rows returned by the query.

QueryBuilder
query.from( "users" ).count();
SELECT COUNT(*) AS aggregate 

FROM `users`
SELECT COUNT(*) FROM [users]

max

Name

Type

Required

Default

Description

column

string

true

The column on which to find the max.

options

struct

false

{}

Any additional queryExecute options.

Returns the maximum value for the given column.

QueryBuilder
query.from( "users" ).max( "age" );
SQL (MySQL)
SELECT MAX(age) AS aggregate FROM `users`

min

Name

Type

Required

Default

Description

column

string

true

The column on which to find the min.

options

struct

false

{}

Any additional queryExecute options.

Returns the minimum value for the given column.

QueryBuilder
query.from( "users" ).min( "age" );
SQL (MySQL)
SELECT MIN(age) AS aggregate FROM `users`

sum

Name

Type

Required

Default

Description

column

string

true

The column to sum.

options

struct

false

{}

Any additional queryExecute options.

Returns the sum of all returned rows for the given column.

QueryBuilder
query.from( "employees" ).sum( "salary" );
SQL (MySQL)
SELECT SUM(salary) AS aggregate FROM `employees`
PreviousRetrieving ResultsNextInserts, Updates, and Deletes

Last updated 5 years ago

Was this helpful?