[TOC]
### 創建查詢生成器
~~~php
$db = \Lying::$maker->db;
$query = $db->query();
~~~
### 使用查詢生成器
~~~php
$db = \Lying::$maker->db;
$query = $db->query();
//select `id`,`username`,`sex` from `user` where id=1
$query->select('id,username,sex')
->from('user')
->where('id=1');
//select `id`,`username` as `name`,`sex` from `user` where id=1
$query->select(['id', 'name'=>'username', 'sex'])
->from(['user'])
->where(['id'=>1]);
//SELECT `u`.`id`, `u`.`username` AS `name`, `sex` FROM `user` AS `u` WHERE `id`=1
$query->select(['u.id', 'name'=>'u.username', 'u.sex'])
->from(['u'=>'user'])
->where(['id'=>1]);
//支持的查詢方法,具體使用方式請看方法詳解
$query->select()
->distinct()
->from()
->where()
->andWhere()
->orWhere()
->join()
->leftJoin()
->rightJoin()
->innerJoin()
->groupBy()
->having()
->andHaving()
->orHaving()
->orderBy()
->limit()
->union();
~~~
> 具體使用方式請看方法詳解 [查詢方法詳解](queryMethod.md)
### 獲取查詢SQL和將要綁定的參數
~~~php
$db = \Lying::$maker->db;
$query = $db->query();
//select `id`,`username`,`sex` from `user` where id=1
$query->select('id,username,sex')
->from('user')
->where(['id'=>1]);
list($sql, $params) = $query->build();
//取出來的sql并不是規范的sql,但是可以用此方式執行:
$db->prepare($sql, $params)->one();
~~~
### 獲取查詢結果
~~~php
$db = \Lying::$maker->db;
$query = $db->query();
//select `id`,`username`,`sex` from `user` where id=1
$query->select('id,username,sex')
->from('user')
->where(['id'=>1]);
$query->one(); //返回結果集中的一條記錄
$query->all(); //返回所有查詢結果的數組
$query->scalar(); //從結果集中的下一行返回單獨的一個字段值,查詢結果為標量
$query->column(); //從結果集中的取出第N列的值
$query->count(); //獲取指定列的記錄數
$query->sum(); //獲取指定列所有值之和
$query->max(); //獲取指定列的最大值
$query->min(); //獲取指定列的最小值
$query->avg(); //獲取指定列的平均值
$query->inc(); //字段值自增
$query->dec(); //字段值自減
~~~
### 插入/更新/刪除操作
#### 插入數據
插入單條數據
~~~php
$db = \Lying::$maker->db;
$query = $db->query();
$data = [
'username' => 'user1',
'sex' => 1,
'password' => '123456',
];
$rows = $query->insert('user', $data); //insert into
$rows = $query->insert('{{%user}}', $data); //use table name with prefix
$rows = $query->insert('user', $data, true); //replace into
~~~
批量插入數據
~~~php
$db = \Lying::$maker->db;
$query = $db->query();
$fields = ['name', 'sex'];
$datas = [
['user1', 1],
['user2', 0],
['user3', 1],
];
$rows = $query->batchInsert('user', $fields, $datas); //insert into
$rows = $query->batchInsert('{{%user}}', $fields, $datas); //use table name with prefix
$rows = $query->batchInsert('user', $fields, $datas, true); //replace into
~~~
> 請注意,批量插入使用拼接sql的方式,受限于sql語句的最大長度!
#### 更新數據
~~~php
$db = \Lying::$maker->db;
$query = $db->query();
$where = ['>', 'id', 100];
$data = ['money'=>'500'];
$rows = $query->update('user', $data, $where);
//或者
$rows = $query->where($where)->update('{{%user}}', $data); //use table name with prefix
~~~
> where的使用方式請參考where函數的詳細說明
#### 刪除操作
~~~php
$db = \Lying::$maker->db;
$query = $db->query();
$where = ['id'=>100];
$rows = $query->delete('user', $where);
//或者
$rows = $query->where($where)->delete('{{%user}}'); //use table name with prefix
~~~
> where的使用方式請參考where函數的詳細說明
### 函數參考
~~~php
/**
* 設置要查詢的字段
* ```php
* select('id, lying.sex, count(id) as count')
* select(['id', 'lying.sex', 'count'=>'count(id)', 'q'=>$query])
* 其中$query為Query實例,必須指定子查詢的別名,只有$columns為數組的時候才支持子查詢
* 注意:當你使用到包含逗號的數據庫表達式的時候,你必須使用數組的格式,以避免自動的錯誤的引號添加
* select(["CONCAT(first_name, ' ', last_name) AS full_name", 'email']);
* ```
* @param string|array $columns 要查詢的字段,當沒有設置要查詢的字段的時候,默認為'*'
* @return $this
*/
public function select($columns);
~~~
* * * * *
~~~php
/**
* 去除重復行
* @param bool $distinct 是否去重
* @return $this
*/
public function distinct($distinct = true);
~~~
* * * * *
~~~php
/**
* 設置要查詢的表
* ```php
* from('user, lying.admin as ad')
* from(['user', 'ad'=>'lying.admin', 'q'=>$query])
* 其中$query為Query實例,必須指定子查詢的別名,只有$tables為數組的時候才支持子查詢
* ```
* @param string|array $tables 要查詢的表
* @return $this
*/
public function from($tables);
~~~
* * * * *
~~~php
/**
* 設置表連接,可多次調用
* @param string $type 連接類型,可以為'left join','right join','inner join'
* @param string|array $table 要連接的表,子查詢用數組形式表示,鍵為別名,值為Query實例
* @param string|array $on 條件,如果要使用'字段1 = 字段2'的形式,請用字符串帶入,用數組的話'字段2'將被解析為綁定參數
* @param array $params 綁定的參數,應為key=>value形式
* @return $this
*/
public function join($type, $table, $on = null, $params = []);
~~~
* * * * *
~~~php
/**
* 設置表左連接,可多次調用
* @param string|array $table 要連接的表,子查詢用數組形式表示,鍵為別名,值為Query實例
* @param string|array $on 條件,如果要使用'字段1 = 字段2'的形式,請用字符串帶入,用數組的話'字段2'將被解析為綁定參數
* @param array $params 綁定的參數,應為key=>value形式
* @return $this
*/
public function leftJoin($table, $on = null, $params = []);
~~~
* * * * *
~~~php
/**
* 設置表右連接,可多次調用
* @param string|array $table 要連接的表,子查詢用數組形式表示,鍵為別名,值為Query實例
* @param string|array $on 條件,如果要使用'字段1 = 字段2'的形式,請用字符串帶入,用數組的話'字段2'將被解析為綁定參數
* @param array $params 綁定的參數,應為key=>value形式
* @return $this
*/
public function rightJoin($table, $on = null, $params = []);
~~~
* * * * *
~~~php
/**
* 設置表連接,可多次調用
* @param string|array $table 要連接的表,子查詢用數組形式表示,鍵為別名,值為Query實例
* @param string|array $on 條件,如果要使用'字段1 = 字段2'的形式,請用字符串帶入,用數組的話'字段2'將被解析為綁定參數
* @param array $params 綁定的參數,應為key=>value形式
* @return $this
*/
public function innerJoin($table, $on = null, $params = []);
~~~
* * * * *
~~~php
/**
* 設置查詢條件
* ```php
* 如果要使用'字段1 = 字段2'的形式,請用字符串帶入,用數組的話'字段2'將被解析為綁定參數
* where("user.id = admin.id and name = :name", [':name'=>'lying']);
* where(['id'=>1, 'name'=>'lying']);
* where(['id'=>[1, 2, 3], ['or', 'name'=>'lying', 'sex'=>1]]);
* ```
* @param string|array $condition 要查詢的條件
* @param array $params 當$condition為字符串時,綁定參數的數組
* @return $this
*/
public function where($condition, $params = []);
~~~
* * * * *
~~~php
/**
* 添加AND條件
* @param string|array $condition 要查詢的條件
* @param array $params 當$condition為字符串時,綁定參數的數組
* @return $this
* @see where()
*/
public function andWhere($condition, $params = []);
~~~
* * * * *
~~~php
/**
* 添加OR條件
* @param string|array $condition 要查詢的條件
* @param array $params 當$condition為字符串時,綁定參數的數組
* @return $this
* @see where()
*/
public function orWhere($condition, $params = []);
~~~
* * * * *
~~~php
/**
* 設置分組查詢
* ```php
* groupBy('id, sex');
* groupBy(['id', 'sex']);
* ```
* @param string|array 要分組的字段
* @return $this
*/
public function groupBy($columns);
~~~
* * * * *
~~~php
/**
* 聚合篩選
* @param string|array $condition 參見where()
* @param array $params 參見where()
* @return $this
* @see where()
*/
public function having($condition, $params = []);
~~~
* * * * *
~~~php
/**
* 添加AND條件
* @param string|array $condition 要查詢的條件
* @param array $params 當$condition為字符串時,綁定參數的數組
* @return $this
* @see having()
*/
public function andHaving($condition, $params = []);
~~~
* * * * *
~~~php
/**
* 添加OR條件
* @param string|array $condition 要查詢的條件
* @param array $params 當$condition為字符串時,綁定參數的數組
* @return $this
* @see having()
*/
public function orHaving($condition, $params = []);
~~~
* * * * *
~~~php
/**
* 設置排序
* ```php
* orderBy('id, name desc');
* orderBy(['id'=>SORT_DESC, 'name']);
* ```
* @param string|array $columns 要排序的字段和排序方式
* @return $this
*/
public function orderBy($columns);
~~~
* * * * *
~~~php
/**
* 設置限制查詢的條數
* ```php
* limit(10);
* limit(5, 20);
* ```
* @param int $offset 偏移的條數,如果只提供此參數,則等同于limit(0, $offset)
* @param int $limit 限制的條數
* @return $this
*/
public function limit($offset, $limit = null);
~~~
* * * * *
~~~php
/**
* 設置聯合查詢,可多次使用
* @param Query $query 子查詢
* @param bool $all 是否使用UNION ALL,默認false
* @return $this
*/
public function union(Query $query, $all = false);
~~~
* * * * *
~~~php
/**
* 組建SQL語句
* @param array $params 傳入參數
* @return array 返回[$statement, $params]
*/
public function build($params = []);
~~~
* * * * *
~~~php
/**
* 返回結果集中的一條記錄
* @param bool $obj 是否返回對象(默認返回關聯數組)
* @param string $class 要實例化的對象,不寫默認為匿名對象
* @return mixed 成功返回查詢結果,失敗返回false
*/
public function one($obj = false, $class = null);
~~~
* * * * *
~~~php
/**
* 返回所有查詢結果的數組
* @param bool $obj 是否返回對象(默認返回關聯數組)
* @param string $class 要實例化的對象,不寫默認為匿名對象
* @return mixed 成功返回查詢結果,失敗返回false
*/
public function all($obj = false, $class = null);
~~~
* * * * *
~~~php
/**
* 從結果集中的下一行返回單獨的一個字段值,查詢結果為標量
* @param int $columnNumber 你想從行里取回的列的索引數字,以0開始
* @return mixed 返回查詢結果,查詢結果為標量
*/
public function scalar($columnNumber = 0);
~~~
* * * * *
~~~php
/**
* 從結果集中的取出第N列的值
* @param int $columnNumber 你想從行里取回的列的索引數字,以0開始
* @return mixed 返回查詢結果
*/
public function column($columnNumber = 0);
~~~
* * * * *
~~~php
/**
* 獲取指定列的記錄數
* @param string $column 要統計的列名
* @return bool|int 返回記錄數
*/
public function count($column = '*');
~~~
* * * * *
~~~php
/**
* 獲取指定列所有值之和
* @param string $column 要相加的列名
* @return bool|float 返回相加后的值
*/
public function sum($column);
~~~
* * * * *
~~~php
/**
* 獲取指定列的最大值
* @param string $column 計算的列名
* @return bool|float 最大值
*/
public function max($column);
~~~
* * * * *
~~~php
/**
* 獲取指定列的最小值
* @param string $column 計算的列名
* @return bool|float 最小值
*/
public function min($column);
~~~
* * * * *
~~~php
/**
* 獲取指定列的平均值
* @param string $column 計算的列名
* @return bool|float 平均值
*/
public function avg($column);
~~~
* * * * *
~~~php
/**
* 字段值自增
* ```php
* $query->from('user')->where(['id'=>1])->inc('num');
* ```
* @param string $field 字段名
* @param int $num 自增的值,必須為正整數
* @return bool|int 成功返回受影響的行數,失敗返回false
*/
public function inc($field, $num = 1);
~~~
* * * * *
~~~php
/**
* 字段值自減
* ```php
* $query->from('user')->where(['id'=>1])->dec('num');
* ```
* @param string $field 字段名
* @param int $num 自減的值,必須為正整數
* @return bool|int 成功返回受影響的行數,失敗返回false
*/
public function dec($field, $num = 1);
~~~
* * * * *
~~~php
/**
* 插入一條數據
* @param string $table 要插入的表名
* @param array $datas 要插入的數據,(name => value)形式的數組
* 當然value可以是子查詢,Query的實例,但是查詢的表不能和插入的表是同一個
* @param bool $replace 是否用REPLACE INTO
* @return int|bool 返回受影響的行數,有可能是0行,失敗返回false
*/
public function insert($table, $datas, $replace = false);
~~~
* * * * *
~~~php
/**
* 批量插入數據
* ```php
* batchInsert('user', ['name', 'sex'], [
* ['user1', 1],
* ['user2', 0],
* ['user3', 1],
* ])
* ```
* @param string $table 要插入的表名
* @param array $columns 要插入的字段名
* @param array $datas 要插入的數據,應為一個二維數組
* @param bool $replace 是否用REPLACE INTO
* @return int|bool 返回受影響的行數,有可能是0行,失敗返回false
*/
public function batchInsert($table, $columns, $datas, $replace = false);
~~~
* * * * *
~~~php
/**
* 更新數據
* @param string $table 要更新的表名
* @param array $datas 要更新的數據,(name => value)形式的數組
* 當然value可以是子查詢,Query的實例,但是查詢的表不能和更新的表是同一個
* @param string|array $condition 更新的條件,參見where()
* @param array $params 條件的參數,參見where()
* @return int|bool 返回受影響的行數,有可能是0行,失敗返回false
*/
public function update($table, $datas, $condition = '', $params = []);
~~~
* * * * *
~~~php
/**
* 刪除數據
* @param string $table 要刪除的表名
* @param string|array $condition 刪除的條件,參見where()
* @param array $params 條件的參數,參見where()
* @return int|bool 返回受影響的行數,有可能是0行,失敗返回false
*/
public function delete($table, $condition = '', $params = []);
~~~
- 序言
- 更新日志
- 安裝
- 規范
- 常量
- 配置
- 自動加載
- MVC
- 模塊
- 控制器
- 模型
- 視圖
- php原生模板
- 模板引擎
- 變量輸出
- 模板注釋
- 模板繼承
- 模板引用
- 流程控制
- 原樣輸出
- 服務組件
- Hook組件
- Request組件
- Router組件
- Cookie組件
- Encrypter組件
- Dispatch組件
- Response組件
- View組件
- Session組件
- Helper組件
- 數據分頁
- 數據驗證
- Logger組件
- Cache組件
- Redis組件
- Connection組件
- 執行sql語句
- 查詢生成器
- 查詢方法詳解
- Schema
- Captcha組件
- CLI
- CLI工具
- 事件
- 類事件
- 實例事件
- 全局事件
- 助手函數
- 擴展
- 異常
- 部署
- Apache
- Nginx
- IIS
- 虛擬主機