SELECT
, (*13)
Columns can be specified in the select()
or in col()
(or with its
alias field()
)., (*14)
Column with optional alias name,, (*15)
// SELECT `user_name` AS `n` FROM `users`
$query = $users->select('user_name', 'n');
Multiple columns,, (*16)
// SELECT `id`, `user_name` AS `n` FROM `users`
$query = $users->select()->col(['id', 'user_name' => 'n']);
// same as above
$query = $users->select()->col('id')->field('user_name', 'n');
Raw mode,, (*17)
// SELECT COUNT(user_id) AS `cnt` FROM `users`
$query = $users->select()->colRaw(['COUNT(user_id)' => 'cnt']);
Common functions like count()
, min()
, max()
, avg()
, sum()
and
sumDistinct()
can be used in the columns., (*18)
// SELECT COUNT(`user_id`) AS `cnt`, MAX(`user_id`) AS `max_id` FROM `users`
$query = $users->select()->count('user_id', 'cnt')->max('user_id', 'max_id');
Generic functions by using func($template, $colName, $colAlias)
,, (*19)
// SELECT CONCAT(`user_name`, "XXX") AS `new_name` FROM `users`
$query = $users->select()->func('CONCAT(%s, "XXX")', 'user_name', 'new_name');
DISTINCT
can be specified with distinct()
, (*20)
// SELECT DISTINCT `user_alias` FROM `users`
$query = $users->select('user_alias')->distinct();
FROM
can used with builder object or select object., (*21)
Use select(false)
to ignore default table from the builder,, (*22)
// SELECT * FROM `sales` AS `s`
$query = $users->select(false)->from('sales', 's');
Builder tables are carried over,, (*23)
// SELECT * FROM `users`, `sales`
$query = $users->select()->from('sales');
Multiple tables (with aliases) supported,, (*24)
// SELECT * FROM `users` AS `u`, `accounts` AS `a`
$query = $users->select()->from(['users' => 'u', 'accounts' => 'a']);
Subqueries can be used in from()
,, (*25)
// builder without default table[s]
$builder = $users->table(false);
// SELECT * FROM (SELECT `user_id` FROM `oldusers`) AS `u`
$query = $builder->select()->from(
$builder->select('user_id')->from('oldusers'), 'u'
);
Single GROUP BY
,, (*26)
// SELECT `group_id`, COUNT(*) AS `cnt` FROM `users` GROUP BY `group_id`
$query = $users->select()->col('group_id')->count('*', 'cnt')->groupBy('group_id');
Multiple groupBy()
and raw mode can be used,, (*27)
// SELECT `group_id`, `age`, COUNT(*) AS `cnt` FROM `users` GROUP BY `group_id`, age ASC
$query = $users->select()->col('group_id')->col('age')->count('*', 'cnt')
->groupBy('group_id')->groupByRaw('age ASC');
Join with another table with same column name, (*28)
// SELECT * FROM `users` INNER JOIN `accounts` ON `users`.`id` = `accounts`.`id`
$query = $users->select()->join('accounts', 'id');
Specify alias for the join table,, (*29)
// SELECT * FROM `users` INNER JOIN `accounts` AS `a` ON `users`.`id` = `a`.`id`
$query = $users->select()->join('accounts a', 'id');
Join table with different column name,, (*30)
// SELECT * FROM `users` INNER JOIN `accounts` AS `a` ON `users`.`id` = `a`.`user_id`
$query = $users->select()->join('accounts a', 'id', 'user_id');
Join with operator specified,, (*31)
// SELECT * FROM `users` INNER JOIN `accounts` AS `a` ON `users`.`id` <> `a`.`user_id`
$query = $users->select()->join('accounts a', 'id', '<>', 'user_id');
Multiple joins,, (*32)
// SELECT * FROM `users`
// INNER JOIN `sales` AS `s` ON `users`.`uid` = `s`.`uid`
// INNER JOIN `order` AS `o` ON `users`.`uid` = `o`.`o_uid`
$query = $users->select()
->join('sales s', 'uid', '=', 'uid')
->join('order o', 'uid', 'o_uid')
->getStatement();
Subqueries in join,, (*33)
// SELECT * FROM `users` INNER JOIN (SELECT `uid` FROM `oldusers`) AS `x`
// ON `users`.`uid` = `x`.`uid`
$query = $users->select()->join(
$builder->select('uid')->from('oldusers')->alias('x'),
'uid'
);
Other joins outerJoin()
, leftJoin()
, leftOuterJoin()
, rightJoin()
,
rightOuterJoin()
, fullOuterJoin()
, crossJoin()
are supported. If want
to use your own join, realJoin()
is handy., (*34)
// SELECT * FROM `users` OUTER JOIN `accounts` AS `a` ON `users`.`id` = `a`.`id`
$query = $users->select()->outerJoin('accounts a', 'id');
// SELECT * FROM `users` NATURAL JOIN `accounts` AS `a` ON `users`.`id` = `a`.`id`
$query = $users->select()->realJoin('NATURAL', 'accounts a', 'id');
LIMIT
and OFFSET
are supported,, (*35)
// SELECT * FROM `users` LIMIT 30 OFFSET 10
$query = $users->select()->limit(30, 10);
// SELECT * FROM `users` LIMIT 20 OFFSET 15
$query = $users->select()->limit(20)->offset(15);
Or use page($pageNum, $pageLength)
,, (*36)
// SELECT * FROM `users` LIMIT 30 OFFSET 60
$query = $users->select()->page(3, 30);
Order by ASC or DESC, (*37)
// SELECT * FROM `users` ORDER BY `age` ASC, `score` DESC
$query = $users->select()->orderByAsc('age')->orderByDesc('score');
Or raw mode, (*38)
// SELECT * FROM `users` ORDER BY age ASC, score DESC
$query = $users->select()->orderByRaw('age ASC, score DESC');
Simple wheres,, (*39)
// SELECT * FROM `users` WHERE age > 18
$query = $users->select()->where('age > 18');
// SELECT * FROM `users` WHERE `age` = 18
$query = $users->select()->where('age', 18);
// SELECT * FROM `users` WHERE `age` < 18
$query = $users->select()->where('age', '<', 18);
Multiple wheres,, (*40)
// SELECT * FROM `users` WHERE `age` > 18 AND `gender` = 'male'
$query = $users->select()->where(['age' => ['>', 18], 'gender' => 'male']);
// same as above
$query = $users->select()->where('age', '>', 18)->where('gender','male');
Complex where,, (*41)
// SELECT * FROM `users` WHERE (`id` = 1 OR (`id` < 20 OR `id` > 100))
// OR `name` = 'Tester'
$query = $users->select()->where(
$builder->expr()->where('id', 1)->orWhere(
$builder->expr()->where('id', '<', 20)->orWhere('id', '>', 100)
)
)->orWhere('name', 'Tester');
Raw mode,, (*42)
// SELECT * FROM `users` WHERE age = 18 OR score > 90
$query = $users->select()->whereRaw('age = 18')->orWhereRaw('score > 90');
with NOT
,, (*43)
// SELECT * FROM `users` WHERE NOT `age` = 18 OR NOT `score` > 90
$query = $users->select()->whereNot('age', 18)->orWhereNot('score', '>', 90);
Where IN
and BETWEEN
, (*44)
// SELECT * FROM `users` WHERE `age` IN (10,12,15,18,20) OR `score` NOT BETWEEN 90 AND 100
$query = $users->select()->whereIn('age', [10,12,15,18,20])
->orWhereNotBetween('score', 90, 100);
Where IS NULL
and IS NOT NULL
, (*45)
// SELECT * FROM `users` WHERE `age` IS NULL OR `score` IS NOT NULL
$query = $users->select()->whereNull('age')->orWhereNotNull('score');
Exists,, (*46)
$qry1 = $users->select('user_id')->where('age', '>', 60);
$sales = $users->table('sales');
// SELECT * FROM `sales` WHERE EXISTS (SELECT `user_id` FROM `users`
// WHERE `age` > 60)
$sql = $sales->select()->whereExists($qry1)->getStatement();
Similar to WHERE
clause,, (*47)
// SELECT * FROM `users` HAVING `age` = 10 OR `level` > 20
$query = $users->select()->having('age', 10)->orHaving('level', '>', 20);
// SELECT * FROM `users` UNION SELECT * FROM `oldusers1`
// UNION ALL SELECT `user_id` FROM `oldusers2`
$sql = $users->select()
->union()
->select()->from('oldusers1')
->unionAll()
->select('user_id')->from('oldusers2')
->getStatement()
INSERT
, (*48)
Single insert statement,, (*49)
// INSERT INTO `users` (`uid`, `uname`) VALUES (2, 'phossa')
$sql = $users->insert()->set('uid', 2)->set('uname', 'phossa')
->getStatement();
// same as above, with array notation
$sql = $users->insert()->set(['uid' => 2, 'uname' => 'phossa'])
->getStatement();
Multiple data rows,, (*50)
// INSERT INTO `users` (`uid`, `uname`) VALUES (2, 'phossa'), (3, 'test')
$query = $users->insert()
->set(['uid' => 2, 'uname' => 'phossa'])
->set(['uid' => 3, 'uname' => 'test']);
Insert with DEFAULT
values, (*51)
// INSERT INTO `users` (`uid`, `uname`, `phone`)
// VALUES (2, 'phossa', DEFAULT), (3, 'test', '1234')
$query = $users->insert([
['uid' => 2, 'uname' => 'phossa'],
['uid' => 3, 'uname' => 'test', 'phone' => '1234']
]);
Insert NULL
instead of default values,, (*52)
// INSERT INTO `users` (`uid`, `uname`, `phone`)
// VALUES (2, 'phossa', NULL), (3, 'test', '1234')
$sql = $query->getStatement(['useNullAsDefault' => true]);
Insert with SELECT
subquery,, (*53)
// INSERT INTO `users` (`uid`, `uname`)
// SELECT `user_id`, `user_name` FROM `oldusers`
$query = $users->insert()->set(['uid', 'uname'])
->select(['user_id', 'user_name'])
->from('oldusers');