(extends Query_Builder_Where)
The Query_Builder_Select class handles all the select operations for the query building process. It extends the
Query_Builder_Where class, so all the methods are inherited.
The distinct method sets whether to select distinct values.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$value |
bool |
true
|
set to false if you don't want to select distinct values |
|
Returns |
Returns the current instance. |
Example |
$query = DB::select('name')->from('users');
$query->distinct();
|
The select method appends columns to select.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$columns |
mixed |
true
|
column name or array($column, $alias) |
|
Returns |
Returns the current instance. |
Example |
$query = DB::select('name')->from('users');
$query->select('surname', 'email');
$query->select(
array('is_active', 'active'),
'birthdate'
);
|
The select_array method appends columns to select.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$columns |
array |
required |
an array containing column names |
$reset |
bool |
false
|
If true, do not append, but overwrite |
|
Returns |
Returns the current instance. |
Example |
$query = DB::select('name')->from('users');
$query->select('surname', 'email');
$query->select_array(array(
array('is_active', 'active'),
'birthdate'
));
$query->select_array(array(
array('surname', 'name'),
'birthdate'
), true);
|
The from method appends tables to select from.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$tables |
mixed |
required |
table names or array($table, $alias) |
|
Returns |
Returns the current instance. |
Example |
$query = DB::select()->from('users');
$query->from(
'admins',
array('comments', 'cmmnts')
);
|
The join method appends tables to join.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$table |
mixed |
required |
table name or array($table, $alias) |
$type |
mixed |
true
|
join type (LEFT, RIGHT, INNER, etc) |
|
Returns |
Returns the current instance. |
Example |
$query = DB::select()->from('users');
$query->join('profiles');
$query = DB::select()->from('users');
$query->join('pictures', 'RIGHT OUTER');
|
The on method adds "ON ..." conditions for the last created JOIN statement.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$c1 |
mixed |
required |
table name or array($table, $alias) |
$op |
string |
required |
logical operator |
$c2 |
mixed |
required |
table name or array($table, $alias) |
|
Returns |
Returns the current instance. |
Example |
$query = DB::select()->from('users');
$query->join('profiles');
$query->on('users.id', '=', 'profiles.user_id');
|
The and_on method is an alias for on.
The on method adds "OR ON ..." conditions for the last created JOIN statement.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$c1 |
mixed |
required |
table name or array($table, $alias) |
$op |
string |
required |
logical operator |
$c2 |
mixed |
required |
table name or array($table, $alias) |
|
Returns |
Returns the current instance. |
Example |
$query = DB::select()->from('users');
$query->join('profiles');
$query->on('users.id', '=', 'profiles.user_id');
$query->or_on('users.id', '=', 'profiles.other_id');
|
The group_by method creates a "GROUP BY ..." filter.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$columns |
mixed |
required |
table name |
|
Returns |
Returns the current instance. |
Example |
$query = DB::select()->from('articles');
$query->group_by('genre', 'category');
|
The having method is an alias for and_having.
The and_having method appends a "AND HAVING" statement.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$column |
string |
required |
Column name or array($column, $alias), object or callback |
$op |
string |
null
|
Logic operator: =, !=, IN, BETWEEN and LIKE. |
$value |
mixed |
null
|
Column value |
|
Returns |
Returns the current instance. |
Example |
$query = DB::select('*')->from('users');
$query->having('name', '!=', 'John');
$query->and_having('surname', '=', 'Doe');
$query->and_having(function($query){
$query->having('email', 'info@example.com');
$query->or_having('email', 'second@example.com');
});
|
The or_having method appends a "OR HAVING" statement.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$column |
string |
required |
Column name or array($column, $alias), object or callback |
$op |
string |
null
|
Logic operator: =, !=, IN, BETWEEN and LIKE. |
$value |
mixed |
null
|
Column value |
|
Returns |
Returns the current instance. |
Example |
$query = DB::select('*')->from('users');
$query->having('name', '!=', 'John');
$query->or_having('surname', '=', 'Doe');
$query->or_having(function($query){
$query->having('email', 'info@example.com');
$query->and_having('email', 'second@example.com');
});
|
The having_open method is an alias for and_having_open.
The and_having_open method opens an SQL closure and appends it using AND.
Static |
No |
Parameters |
None
|
Returns |
Returns the current instance. |
Example |
$query = DB::select('*')->from('users');
$query->having('name', '=', 'john')
$query->and_having_open();
|
The or_having_open method opens an SQL closure and appends it using OR.
Static |
No |
Parameters |
None
|
Returns |
Returns the current instance. |
Example |
$query = DB::select('*')->from('users');
$query->having('name', '=', 'john')
$query->or_having_open();
|
The having_close method is an alias for and_having_close.
The and_having_close method closes an SQL closure.
Static |
No |
Parameters |
None
|
Returns |
Returns the current instance. |
Example |
$query = DB::select('*')->from('users');
$query->and_having_open();
$query->having('name', '=', 'john');
$query->and_having_close();
|
The or_having_close method closes an SQL closure.
Static |
No |
Parameters |
None
|
Returns |
Returns the current instance. |
Example |
$query = DB::select('*')->from('users');
$query->having('email', 'like', '%@example.com');
$query->or_having_open();
$query->having('name', '=' 'John');
$query->and_having('surname', '=', 'Doe');
$query->or_having_close();
|
The offset method sets row number to start from when selecting/updating/deleting.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$number |
int |
required |
The row number to start from |
|
Returns |
Returns the current instance |
Example |
$query = DB::select('*')->from('users');
$query->limit(10);
$query->offset(5);
|
The compile method returns the select SQL query as a string.
Static |
No |
Parameters |
Param |
Type |
Default |
Description |
$db |
object |
required |
A database connection |
|
Returns |
Returns the SQL query as a string. |
Example |
$query = DB::select('*')->from('users');
$query->limit(10);
$query->offset(5);
$connection = Database_Connection::instance();
$sql = $query->compile($connection);
|
The reset method resets all values of the current instance.
Static |
No |
Parameters |
None
|
Returns |
Returns the current instance. |
Example |
$query = DB::select('*')->from('users');
$query->where('name', 'bob');
$query->where('surname', 'unknown');
$query->reset();
$query->select('email')->from('admins')->where('role', 'superadmin');
|