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