MysqlModel Mysql 数据库服务模块类 > MysqlSqlBuilder
⚠️ MysqlSqlBuilder 构造函数已私有,无法通过 new 方式实例化,仅通过 MysqlModel 的
getSqlBuilder
方法获得
$query = $mysqlModel->getSqlBuilder()
->select('*')
->from('users u')
->join('users_info ui', 'uid')
->where('status', true)
->whereGroupStart()
->where('username LIKE', 'bodao%')
->orWhere('username LIKE', 'ymb%')
->whereGroupEnd()
->groupBy('role')
->orderBy('username DESC');
// SQL
// SELECT *
// FROM `users` AS `u`
// INNER JOIN `users_info` AS `ui` USING (uid)
// WHERE `status` = "1" AND
// ( `username` LIKE "bodao%" OR `username` LIKE "ymb%" )
// GROUP BY role
// ORDER BY username DESC
MysqlSqlBuilder::select
设置 SELECT
MysqlSqlBuilder MysqlSqlBuilder::select ( string $select [, bool $escape = true] )
参数
$select - SELECT 字段列表,若有多个字段,用,
分隔
$escape - 是否 SQL 转义,默认true
返回值
当前MysqlSqlBuilder
对象范例
$query->select('foo, bar');
$query->select('FROM_UNIXTIME(`created`)', false);
MysqlSqlBuilder::distinct
设置 DISTINCT
MysqlSqlBuilder MysqlSqlBuilder::distinct ( [bool $distinct = true] )
参数
$distinct - 是否设置 DISTINCT,默认true
返回值
当前MysqlSqlBuilder
对象范例
$query->distinct();
MysqlSqlBuilder::from
设置 FROM
MysqlSqlBuilder MysqlSqlBuilder::from ( mixed $from)
参数
$from - 表名,若为字符串,则为表名;若为数组,则数组键为别名,值为表名返回值
当前MysqlSqlBuilder
对象范例
$query->from('table'); // FROM `table`
$query->from('table t'); // FROM `table` AS `t`
$query->from([ 'u' => 'user' ]); // FROM `user` AS `u`
MysqlSqlBuilder::join
设置 JOIN
MysqlSqlBuilder MysqlSqlBuilder::join ( mixed $join, string $condition [, $type = 'INNER'] )
参数
$join - 表名,若为字符串,则为表名;若为数组,则数组键为别名,值为表名
$condition - 连接条件
$type - 连接方式,默认INNER
内连,可选LEFT
左连,RIGHT
右连返回值
当前MysqlSqlBuilder
对象范例
$query->join('table', 'id'); // INNER JOIN `table` USING(id)
$query->join('table t', 'a.id = t.id'); // INNER JOIN `table` AS `t` ON a.id = t.id
$query->join([ 'u' => 'user' ], 'uid', 'left'); // LEFT JOIN `user` AS u USING(uid)
MysqlSqlBuilder::where
MysqlSqlBuilder::orWhere
设置 WHERE
MysqlSqlBuilder MysqlSqlBuilder::where ( mixed $condition [, mixed $value = null [, $escape = true]] )
MysqlSqlBuilder MysqlSqlBuilder::orWhere ( mixed $condition [, mixed $value = null [, $escape = true]] )
- 参数
$condition - 条件,若为字符串表示查询字段;若为数组,则数组键表示数组,值表示查询值。条件可包含比较运算符
$value - 若$condition
为字符串,该参数表示查询值;若$condition
为数组,该参数被忽略,默认为空
$escape - 是否 SQL 转义,默认为true
比较运算符 | 描述 |
---|---|
= |
相等,默认值 |
<=> |
NULL 安全相等,查看手册
|
!= <>
|
不相等 |
> |
大于 |
>= |
大于等于 |
< |
小于 |
<= |
小于等于 |
IS |
布尔型判断真 |
IS NOT |
布尔型判断假 |
IN |
包含 |
NOT IN |
不包含 |
LIKE |
字符串相似,查看手册 |
NOT LIKE |
字符串不相似 |
返回值
当前MysqlSqlBuilder
对象范例
$query->where('foo', 'bar');
$query->where([ 'foo IN' => [1, 2, 3]]);
$query->orWhere('`created` >', 'UNIX_TIMESTAMP() - 1800', false);
MysqlSqlBuilder::whereGroupStart
MysqlSqlBuilder::orWhereGroupStart
MysqlSqlBuilder::notWhereGroupStart
MysqlSqlBuilder::orNotWhereGroupStart
设置 WHERE 嵌套
MysqlSqlBuilder MysqlSqlBuilder::whereGroupStart ( void )
MysqlSqlBuilder MysqlSqlBuilder::orWhereGroupStart ( void )
MysqlSqlBuilder MysqlSqlBuilder::notWhereGroupStart ( void )
MysqlSqlBuilder MysqlSqlBuilder::orNotWhereGroupStart ( void )
参数
无返回值
当前MysqlSqlBuilder
对象范例
$query->whereGroupStart();
MysqlSqlBuilder::whereGroupEnd
关闭 WHERE 嵌套
MysqlSqlBuilder MysqlSqlBuilder::whereGroupEnd ( void )
参数
无返回值
当前MysqlSqlBuilder
对象范例
$query->whereGroupEnd();
MysqlSqlBuilder::having
MysqlSqlBuilder::orHaving
设置 HAVING
MysqlSqlBuilder MysqlSqlBuilder::having ( mixed $condition [, mixed $value = null [, $escape = true]] )
MysqlSqlBuilder MysqlSqlBuilder::v ( mixed $condition [, mixed $value = null [, $escape = true]] )
参数
$condition - 条件,若为字符串表示查询字段;若为数组,则数组键表示数组,值表示查询值。条件可包含比较运算符,参考where
方法
$value - 若$condition
为字符串,该参数表示查询值;若$condition
为数组,该参数被忽略,默认为空
$escape - 是否 SQL 转义,默认为true
返回值
当前MysqlSqlBuilder
对象范例
$query->having('foo', 'bar');
$query->having([ 'foo IN' => [1, 2, 3]]);
$query->orHaving('`created` >', 'UNIX_TIMESTAMP() - 1800', false);
MysqlSqlBuilder::limit
设置 LIMIT
MysqlSqlBuilder MysqlSqlBuilder::limit ( int $limit [, int $offset = 0] )
参数
$limit - 结果集行数
$offset - 起始行,默认0
返回值
当前MysqlSqlBuilder
对象范例
$query->limit(100, 50);
MysqlSqlBuilder::limitPage
设置 LIMIT,根据页码
MysqlSqlBuilder MysqlSqlBuilder::limitPage ( int $limit [, int $page = 1] )
参数
$limit - 结果集行数
$page - 页码,默认1
返回值
当前MysqlSqlBuilder
对象范例
$query->page(100, 2);
MysqlSqlBuilder::orderBy
设置 ORDER BY
MysqlSqlBuilder MysqlSqlBuilder::orderBy ( mixed $orderby [, bool $escape = true] )
参数
$orderby - ORDER BY 字段列表,若有多个字段,用,
分隔
$escape - 是否 SQL 转义,默认true
返回值
当前MysqlSqlBuilder
对象范例
$query->orderBy('created');
$query->orderBy([ 'foo', 'bar DESC' ]);
$query->orderBy('`foo` - `bar`', false);
MysqlSqlBuilder::groupBy
设置 GROUP BY
MysqlSqlBuilder MysqlSqlBuilder::groupBy ( mixed $groupby [, bool $escape = true] )
参数
$groupby - GROUP BY 字段列表,若有多个字段,用,
分隔
$escape - 是否 SQL 转义,默认true
返回值
当前MysqlSqlBuilder
对象范例
$query->groupBy('created');
$query->groupBy([ 'foo', 'bar DESC' ]);
$query->groupBy('`foo` - `bar`', false);
MysqlSqlBuilder::getSql
构建 SQL 语句
string MysqlSqlBuilder::getSql ( void )
参数
无返回值
SQL 语句范例
$sql = $query->getSql();
MysqlSqlBuilder::query
构建 SQL 并执行
MysqlResult MysqlSqlBuilder::query ( [bool $throwsException = true] )
参数
$throwsException - 是否抛出异常,默认为true
返回值
查询结果对象范例
$result = $query->query();