LogoLogo
9.2.0
9.2.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
      • Interception Points
    • Debugging
      • sqlCommenter
  • Schema Builder
    • Overview
    • Create
    • Columns
    • Column Modifiers
    • Column Constraints
    • Creating Table Constraints
    • Alter
    • Drop
    • Debugging
  • External Links
    • API Docs
    • Source Code
    • Issue Tracker
Powered by GitBook
On this page
  • groupBy
  • having

Was this helpful?

Edit on GitHub
Export as PDF
  1. Query Builder
  2. Building Queries

Group By and Having

groupBy

Name

Type

Required

Default

Description

groups

string | array

true

Passing a single string will group by that one column.

QueryBuilder
query.from( "users" )
    .groupBy( "country" );
MySQL
SELECT *
FROM `users`
GROUP BY `country`

You can also pass a list of column names. A single comma (",") will be used as the delimiter.

QueryBuilder
query.from( "users" )
    .groupBy( "country,city" );
MySQL
SELECT *
FROM `users`
GROUP BY `country`, `city`

An array of column names can be provided.

QueryBuilder
query.from( "users" )
    .groupBy( [ "country", "city" ] );
MySQL
SELECT *
FROM `users`
GROUP BY `country`, `city`

Calling groupBy multiple times will to the current groups.

QueryBuilder
query.from( "users" )
    .groupBy( "country" )
    .groupBy( "city" );
MySQL
SELECT *
FROM `users`
GROUP BY `country`, `city`
QueryBuilder
query.from( "users" )
    .groupBy( query.raw( "DATE(created_at)" ) );
MySQL
SELECT *
FROM `users`
GROUP BY DATE(created_at)

having

Name

Type

Required

Default

Description

column

true

operator

any

false

The operator to use for the constraint (i.e. "=", "<", ">=", etc.). A value can be passed as the operator and the value left null as a shortcut for equals (e.g. where( "column", 1 ) == where( "column", "=", 1 ) ).

value

any

false

combinator

string

false

"and"

The boolean combinator for the clause. Valid options are "and" or "or". Avoid passing this parameter explicitly. Where possible use the andHaving and orHaving methods instead.

Adds a having clause to a query.

QueryBuilder
query.from( "users" )
    .groupBy( "email" )
    .having( "email", ">", 1 );
MySQL
SELECT *
FROM `users`
GROUP BY `email`
HAVING `email` > ?

Expressions can be used in place of the column or the value.

QueryBuilder
query.from( "users" )
    .groupBy( "email" )
    .having( query.raw( "COUNT(email)" ), ">", 1 );
MySQL
SELECT *
FROM `users`
GROUP BY `email`
HAVING COUNT(email) > ?
PreviousOrder ByNextLimit, Offset, and Pagination

Last updated 1 year ago

Was this helpful?

A single column name, a list of column names, or an array of column names to group by. An can be passed as well.

An can be passed in place of a column.

string |

The name of the column or with which to constrain the query.

The value with which to constrain the column. An can be passed as well.

Expression
Expression
Expression
Expression
Expression