Search…
From

from

Name
Type
Required
Default
Description
from
string | Expression
true
The name of the table or a Expression object from which the query is based.
Used to set the base table for the query.
QueryBuilder
1
query.from( "users" );
Copied!
MySQL
1
SELECT * FROM `users`
Copied!
You can optionally specify an alias for the table.
QueryBuilder
1
query.from( "users as u" );
Copied!
MySQL
1
SELECT * FROM `users` AS `u`
Copied!

table

Name
Type
Required
Default
Description
table
string | Expression
true
The name of the table or a Expression object from which the query is based.
An alias for from where you like how calling table looks.
QueryBuilder
1
query.table( "users" ).insert( { "name" = "jon" } );
Copied!
MySQL
1
INSERT INTO `users` (`name`) VALUES (?)
Copied!

fromRaw

Name
Type
Required
Default
Description
from
string
true
The sql snippet to use as the table.
bindings
array
false
[]
Any bindings needed for the expression.
Sometimes you need more control over your from clause in order to add grammar specific instructions, such as adding SQL Server table hints to your queries.
QueryBuilder
1
query.fromRaw( "[users] u (nolock)" ).get();
Copied!
SQL Server
1
SELECT * FROM [users] u (nolock)
Copied!
Since the fromRaw() takes your string verbatim, it's important that you make sure your SQL declaration is escaped properly. Failure to properly escape your table names may result in SQL errors.
Using fromRaw will most likely tie your code to a specific database, so think carefully before using the fromRaw method if you want your project to be database agnostic.
Many database engines allow you to define User Defined Functions. For example, SQL Server allows you to define UDFs that will return a table. In these type of cases, it may be necessary to bind parameters to your from clause.
You can bind parameters to the fromRaw() method by passing a secondary argument that is an array of the parameters to bind.
QueryBuilder
1
query.fromRaw(
2
"dbo.generateDateTable(?, ?, ?) as dt",
3
[ "2017-01-01", "2017-12-31", "m" ]
4
).get();
Copied!
SQL Server
1
SELECT * FROM dbo.generateDateTable(?, ?, ?) as dt
Copied!

fromSub

Name
Type
Required
Default
Description
alias
string
true
The alias for the derived table.
input
Function | QueryBuilder
true
Either a QueryBuilder instance or a closure to define the derived query.
Complex queries often contain derived tables. Derived tables are essentially a temporal table defined as a subquery in the from statement.
QueryBuilder
1
query.select( [ "firstName", "lastName" ] )
2
.fromSub( "legalUsers", function ( q ) {
3
q.select( [ "lName as lastName", "fName as firstName" ] )
4
.from( "users" )
5
.where( "age", ">=", 21 )
6
;
7
} )
8
.orderBy( "lastName" )
9
.get()
Copied!
MySQL
1
SELECT `firstName`, `lastName`
2
FROM (
3
SELECT `lName` as `lastName`, `fName` as `firstName`
4
FROM `users`
5
WHERE `age` >= 21
6
) AS `legalUsers`
7
ORDER BY `lastName`
Copied!
In additional a function callback, a separate QueryBuilder instance can be passed to the fromSub method.
QueryBuilder
1
var legalUsersQuery = query
2
.select( [ "lName as lastName", "fName as firstName" ] )
3
.from( "users" )
4
.where( "age", ">=", 21 );
5
6
query.select( [ "firstName", "lastName" ] )
7
.fromSub( "legalUsers", legalUsersQuery )
8
.orderBy( "lastName" )
9
.get();
Copied!
MySQL
1
SELECT `firstName`, `lastName`
2
FROM (
3
SELECT `lName` as `lastName`, `fName` as `firstName`
4
FROM `users`
5
WHERE `age` >= 21
6
) AS `legalUsers`
7
ORDER BY `lastName`
Copied!
Last modified 1mo ago
Copy link