QueryBuilder

最后更新于:2022-04-02 07:07:41

## Mix\Database\QueryBuilder::class 查询生成器,能帮助 Connection 生成 `select` 类型的语句,便于阅读性与开发效率,能生成大部分常用业务的 SQL。 >[info] insert、batchInsert、update、delete 类型的 SQL 与其他更为复杂的 SQL 依然需使用 Connection 开发 ## Result 任何 Connection 连接对象的 table 方法,都可返回一个 QueryBuilder 实例。 ``` // 返回全部 $ret = $db->table('users')->get(); // 返回第一行 $ret = $db->table('users')->first(); ``` ## Select ``` // 常规 $ret = $db->table('users')->select('name', 'email as user_email')->get(); // 函数 $ret = $db->table('users')->select('count(*)')->first(); ``` ## Join 全部关联方法有: - join - leftJoin - rightJoin - fullJoin ~~~ $ret = $db->table('users') ->join('orders', ['users.id', '=', 'orders.user_id']) ->select('users.*', 'orders.price') ->get(); ~~~ 多个关联条件: > AND ``` ->join('orders', [['users.id', '=', 'orders.user_id'], ['users.name', '=', 'orders.user_name']]) // JOIN orders ON users.id = orders.user_id AND users.name = orders.user_name ``` > OR ``` ->join('orders', [['users.id', '=', 'orders.user_id'], ['or', ['users.name', '=', 'orders.user_name']]]) // JOIN orders ON users.id = orders.user_id OR users.name = orders.user_name ``` ## Where 只有一个 `where` 方法: ``` // 一纬 $ret = $db->table('users')->where(['id', '=', 1])->get(); ``` ``` // 二维 $ret = $db->table('users') ->where([['id', '=', 1], ['name', '=', "Xiao Liu"]]) ->get(); ``` 多个关联条件: > AND ``` $ret = $db->table('users') ->where(['id', '=', 1]) ->where(['name', '=', 'Xiao Liu']) ->get(); ``` > OR ``` $ret = $db->table('users') ->where(['id', '=', 1]) ->where(['or', ['id', '=', 2]]) ->get(); ``` > OR AND ``` $ret = $db->table('users') ->where(['id', '=', 1]) ->where(['or', [['id', '=', 2], ['id', '=', 3]]]) ->get(); // WHERE id = 1 OR (id = 2 AND id = 3) ``` > AND OR ``` $ret = $db->table('users') ->where(['id', '=', 1]) ->where(['and', [['id', '=', 2], ['or', ['id', '=', 3]]]]) ->get(); // WHERE id = 1 AND (id = 2 OR id = 3) ``` ### In / NotIn > IN ``` $ret = $db->table('users') ->where(['id', 'in', [1, 2]]) ->get(); ``` > NOT IN ``` $ret = $db->table('users') ->where(['id', 'not in', [1, 2]]) ->get(); ``` ### Between / NotBetween > BETWEEN ``` // 最新用法 $ret = $db->table('users') ->where(['id', 'between', [5, 10]]) ->get(); ``` > NOT BETWEEN ``` // 最新用法 $ret = $db->table('users') ->where(['id', 'not between', [5, 10]]) ->get(); ``` ### Null / NotNull > IS NULL ~~~ $ret = $db->table('users') ->where(['id', 'is null']) ->get(); ~~~ > IS NOT NULL ~~~ $ret = $db->table('users') ->where(['id', 'is not null']) ->get(); ~~~ ## Order, Group, Limit, Offset ### orderBy 单个: ~~~ $ret = $db->table('users') ->orderBy('id', 'desc') ->get(); ~~~ 多个: ~~~ $ret = $db->table('users') ->orderBy('id', 'desc') ->orderBy('email', 'asc') ->get(); ~~~ ### groupBy / having 单个: ~~~ $ret = $db->table('orders') ->select('user_id', 'count(id) as counts') ->groupBy('user_id') ->having('count(id)', '>=', 1) ->get(); ~~~ 多个: ~~~ $ret = $db->table('orders') ->select('user_id', 'price', 'count(id) as counts') ->groupBy('user_id', 'price') ->having('count(id)', '>=', 1) ->get(); ~~~ ### limit / offset ~~~ $ret = $db->table('users') ->offset(10) ->limit(5) ->get(); ~~~
';