>[danger] 需 mix-database >= v2.0.2 才可使用。 ## QueryBuilder 查询生成器,能帮助 PDOConnection 生成 `select` 类型的语句,便于阅读性与开发效率,能生成大部分常用业务的 SQL。 >[info] insert、batchInsert、update、delete 类型的 SQL 与其他更为复杂的 SQL 依然需使用 PDOConnection 开发。 | 类 | 调用 | | --- | --- | | Mix\Database\QueryBuilder | $db->table('table') | ## 获取连接 - 同步模式 ``` $db = app()->db; ``` - 协程模式 > 通过连接池组件获取。 ``` $db = app()->dbPool->getConnection(); $db->release(); // 不手动释放的连接不会归还连接池,会在析构时丢弃 ``` ## Result 任何 PDOConnection 连接对象的 table 方法,都可返回一个 QueryBuilder 实例。 ``` // 返回全部 $ret = $db->table('users')->get(); // 返回第一行 $ret = $db->table('users')->first(); ``` ## Select ``` $ret = $db->table('users')->select('name', 'email as user_email')->get(); ``` ## 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(); ``` 多个关联条件: > 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(); ~~~