条件构造主要有以下几个方法:`table`,`name`,`fields`,`where`,`whereOr`,`whereIn`,`whereNotIn`,`whereLike`,`whereExists`,`whereBetween` ,`whereNull`,`whereNotNull`,`order`,`group`,`join`,`leftJoin`,`rightJoin`,`limit`。 #### table ```php table(string $table) ``` 如果有前缀则`$table`必须加上前缀 #### name ```php name(string $table) ``` 如果有前缀,则不需要加前缀 #### fields ```php fields($fields) ``` 设置要查询的字段,可以是索引数组,也可以是字符串,不使用该方法默认为`*` #### order ```php order(array $order) ``` ```php \Max\Facade\Db::name('users')->order(['id' => 'DESC','sex' => 'DESC'])->select(); ``` 最终的SQL可能是 ``` SELECT * FROM users ORDER BY id DESC, sex DESC; ``` #### group ```php group(array $group) ``` ```php \Max\Facade\Db::name('users')->group(['sex','id' => 'sex = 1'])->select(); ``` 最终的SQL可能是 ``` SELECT * FROM users GROUP BY sex,id HAVING sex = 1; ``` #### limit ```php limit(int $limit, int $offset = null) ``` ```php \Max\Facade\Db::name('users')->limit(1,3)->select(); \Max\Facade\Db::name('users')->limit(1)->select(); ``` 根据数据库不同最终的SQL可能是 ``` SELECT * FROM users LIMIT 3,1; SELECT * FROM users LIMIT 1; ``` 也可能是 ``` SELECT * FROM users LIMIT 1 OFFSET 3; ``` #### join 联表有提供了三种方式`innerJoin` `leftJoin` `rightJoin` 例如如下语句: ```php \Max\Facade\Db::name('users')->join(['books' => 'books.user_id = users.id'])->select(); \Max\Facade\Db::name('users')->leftJoin(['books' => 'books.user_id = users.id'])->select(); \Max\Facade\Db::name('users')->rightJoin(['books' => 'books.user_id = users.id'])->select(); ``` 最终的SQL可能是 ``` SELECT * FROM users INNER JOIN books on books.user_id = users.id; SELECT * FROM users LEFT JOIN books on books.user_id = users.id; SELECT * FROM users RIGHT JOIN books on books.user_id = users.id; ``` #### where ```php where(array $where, string $operator = '=') ``` 例如我有如下的查询 ```php \Max\Facade\Db::name('users')->where(['id' => 1, 'sex = 0'])->select(); \Max\Facade\Db::name('users')->where(['id' => 2], '>=')->select(); ``` 最终的SQL可能依次如下 ``` SELECT * FROM users WHERE id = ? AND sex = 0; SELECT * FROM users WHERE id >= ?; ``` 可以看到`id = ?` 和 `sex = 0` 说明id这个条件可以经过预处理的,而条件数组的键为数字的却不会被处理。 #### whereLike ```php whereLike(array $whereLike) ``` 例如我有如下的查询 ```php \Max\Facade\Db::name('users')->whereLike(['username' => 1, 'sex = 0'])->select(); ``` 最终的SQL可能如下 ``` SELECT * FROM users WHERE username LIKE ? AND sex = 0; ```