LogoLogo
6.4.0
6.4.0
  • Introduction
  • What's New?
  • Installation & Usage
  • Migration Guide
  • Contributing & Filing Issues
  • Contributors
  • Query Builder
    • Return Format
    • Interception Points
    • Retrieving Results
    • Aggregates
    • Selects
    • From
    • Joins
    • Where Clauses
    • Conditionals
    • Ordering, Grouping & Limit
    • Unions
    • Common Table Expressions (i.e. CTEs)
    • Inserts
    • Updates
    • Deletes
    • New Query
    • Clone
  • Schema Builder
    • Overview
    • Create
    • Columns
    • Column Modifiers
    • Column Constraints
    • Creating Table Constraints
    • Alter
    • Drop
Powered by GitBook
On this page

Was this helpful?

Edit on Git
Export as PDF
  1. Query Builder

Selects

Specifying A Select Clause

Of course, you may not always want to select all columns from a database table. Using the from method, you can specify a custom from clause for the query:

var getResults = query.from('users')
    .get('name,email,age');
writeDump(getResults);

Individual columns can contain fully-qualified names (i.e. "some_table.some_column"), fully-qualified names with table aliases (i.e. "alias.some_column"), and even set column aliases themselves (i.e. "some_column AS c"). Columns can be a single column, a list or columns (comma-separated), or an array of columns.

var getResults = query.from('users')
    .get('name as myAccountName,users.email,age');
writeDump(getResults);
var getResults = query.from('users as myTableAlias')
    .get( columns = ['name as myAccountName' ,'myTableAlias.email' ,'age'], options= { datasource='myOtherDatasource'} );
writeDump(getResults);

The distinct method allows you to force the query to return distinct results:

var getResults = query.from('users')
    .select('email')
    .distinct();
writeDump(getResults);

(Note that distinct applies to the entire query, not just certain fields.)

If you already have a query builder instance and you wish to add a column to its existing select clause, you may use the addSelect method:

var getResults = query.from('users')
    .where('age','>=','18');
getResults = getResults.addSelect('name,email,age').get();
writeDump(getResults);
PreviousAggregatesNextFrom

Last updated 5 years ago

Was this helpful?