## 快捷查询
快捷查询方式是**一种多字段相同查询条件**的简化写法,可以进一步简化查询条件的写法,在多个字段之间用`|`分割表示OR查询,用`&`分割表示AND查询,可以实现下面的查询,例如:
~~~
Db::table('think_user')
->where('name|title','like','thinkphp%')
->where('create_time&update_time','>',0)
->find();
~~~
生成的查询SQL是:
~~~
SELECT * FROM `think_user` WHERE ( `name` LIKE 'thinkphp%' OR `title` LIKE 'thinkphp%' ) AND ( `create_time` > 0 AND `update_time` > 0 ) LIMIT 1
~~~
> 快捷查询支持所有的查询表达式。
## 区间查询
区间查询是一种**同一字段多个查询条件**的简化写法,例如:
~~~
Db::table('think_user')
->where('name',['like','thinkphp%'],['like','%thinkphp'])
->where('id',['>',0],['<>',10],'or')
->find();
~~~
生成的SQL语句为:
~~~
SELECT * FROM `think_user` WHERE ( `name` LIKE 'thinkphp%' AND `name` LIKE '%thinkphp' ) AND ( `id` > 0 OR `id` <> 10 ) LIMIT 1
~~~
> 区间查询的查询条件必须使用数组定义方式,支持所有的查询表达式。
下面的查询方式是错误的:
~~~
Db::table('think_user')
->where('name',['like','thinkphp%'],['like','%thinkphp'])
->where('id',5,['<>',10],'or')
->find();
~~~
## 批量查询
可以进行多个条件的批量条件查询定义,例如:
~~~
Db::table('think_user')
->where([
'name' => ['like','thinkphp%'],
'title' => ['like','%thinkphp'],
'id' => ['>',0],
'status'=> 1
])
->select();
~~~
生成的SQL语句为:
~~~
SELECT * FROM `think_user` WHERE `name` LIKE 'thinkphp%' AND `title` LIKE '%thinkphp' AND `id` > 0 AND `status` = '1'
~~~
## 闭包查询
~~~
Db::table('think_user')->select(function($query){
$query->where('name','thinkphp')
->whereOr('id','>',10);
});
~~~
生成的SQL语句为:
~~~
SELECT * FROM `think_user` WHERE `name` = 'thinkphp' OR `id` > 10
~~~
## 使用Query对象查询
也可以事先封装Query对象,并传入select方法,例如:
~~~
$query = new \think\db\Query;
$query->name('user')
->where('name','like','%think%')
->where('id','>',10)
->limit(10);
Db::select($query);
~~~
> 如果使用`Query`对象的话,`select`方法之前调用的任何的链式操作都是无效。
## 混合查询
可以结合前面提到的所有方式进行混合查询,例如:
~~~
Db::table('think_user')
->where('name',['like','thinkphp%'],['like','%thinkphp'])
->where(function($query){
$query->where('id',['<',10],['>',100],'or');
})
->select();
~~~
生成的SQL语句是:
~~~
SELECT * FROM `think_user` WHERE ( `name` LIKE 'thinkphp%' AND `name` LIKE '%thinkphp' ) AND ( `id` < 10 or `id` > 100 )
~~~
## 字符串条件查询
对于一些实在复杂的查询,也可以直接使用原生SQL语句进行查询,例如:
~~~
Db::table('think_user')
->where('id > 0 AND name LIKE "thinkphp%"')
->select();
~~~
为了安全起见,我们可以对字符串查询条件使用参数绑定,例如:
~~~
Db::table('think_user')
->where('id > :id AND name LIKE :name ',['id'=>0, 'name'=>'thinkphp%'])
->select();
~~~
`V5.0.4+`开始,ThinkPHP支持对同一个字段多次调用查询条件,例如:
~~~
Db::table('think_user')
->where('name','like','%think%')
->where('name','like','%php%')
->where('id','in',[1,5,80,50])
->where('id','>',10)
->find();
~~~
- 简介
- PHP的优势
- PHP运行原理和机制
- PHP7有哪些新特性
- 第一章 PHP
- 基础
- 1、流程控制
- if...else
- switch
- for
- while
- foreach
- 跳出循环
- 2、数组函数
- 数组排序
- sort() - 以正序数组排序
- rsort() - 以降序对数组排序
- asort() - 根据值,以升序对关联数组进行排序
- ksort() - 根据键,以升序对关联数组进行排序
- arsort() - 根据值,以降序对关联数组进行排序
- krsort() - 根据键,以降序对关联数组进行排序
- 数组函数
- array_column() - 返回输入数组中某个单一列的值
- array_merge() - 把一个或多个数组合并为一个数组
- array_search() - 搜索数组中给定的值并返回键名
- array_shift() - 删除数组中首个元素,并返回被删除元素的值
- array_pop() - 删除数组的最后一个元素(出栈)
- array_product() - 计算数组中所有值的乘积
- array_push() - 将一个或多个元素插入数组的末尾(入栈)
- array_rand() - 返回数组中一个或多个随机的键
- array_sum() - 返回数组中值的和
- array_unique() - 删除数组中的重复值
- array_unshift() - 在数组开头插入一个或多个元素
- array_values() - 返回数组中所有的值
- in_array() - 检查数组中是否存在指定的值
- list() - 把数组中的值赋给一些变量
- 3、字符串函数
- strlen() - 返回字符串长度
- substr() - 截取字符串
- str_replace() - 把字符串的一部分替换为另一个字符串
- str_ireplace() - 替换字符串中的一些字符(对大小写不敏感)
- strstr() - 查找字符串在另一字符串中的第一次出现(对大小写敏感)
- stristr() - 查找字符串在另一字符串中第一次出现的位置(大小写不敏感)
- strripos() - 查找字符串在另一字符串中最后一次出现的位置(对大小写不敏感)
- strrpos() - 查找字符串在另一字符串中最后一次出现的位置(对大小写敏感)
- trim() - 移除字符串两侧的空白字符和其他字符
- rtrim() - 移除字符串右侧的空白字符或其他字符
- ltrim() - 移除字符串左侧的空白字符或其他字符
- strrev() - 反转字符串
- explode() - 把字符串打散为数组
- implode() - 数组转换为字符串
- strip_tags() - 剥去字符串中的 HTML 和 PHP 标签
- strtolower() - 把字符串转换为小写字母
- strtoupper() - 把字符串转换为大写字母
- htmlentities() - 把字符转换为 HTML 实体
- html_entity_decode() - 把 HTML 实体转换为字符
- md5() - 计算字符串的 MD5 散列
- md5_file() - 计算文件的 MD5 散列
- sha1() - 计算字符串的 SHA-1 散列
- sha1_file() - 计算文件的 SHA-1 散列
- 4、日期时间
- date() - 函数把时间戳格式化为更易读的日期和时间
- mktime() - 返回日期的 Unix 时间戳
- strtotime - 转化为时间戳
- 第二章 MYSQL
- 简介
- connect - 连接数据库
- create - 创建数据库和表
- insert - 添加数据
- select - 查询数据
- update - 修改数据
- delect - 删除数据
- where - 条件
- order by - 关键词
- 第三章 编辑器
- phpstorm
- 第四章 框架
- thinkphp5.0
- tp安装
- 开发规范
- 目录结构
- 数据库
- 查询数据
- 添加数据
- 更新数据
- 删除数据
- 查询方法
- 查询语法
- 链式操作
- where
- table
- alias
- field
- order
- limit
- page
- group
- having
- join
- union
- distinct
- lock
- cache
- comment
- fetchSql
- force
- bind
- partition
- strict
- failException
- sequence
- 聚合查询
- 时间查询
- 高级查询
- 视图查询
- 子查询
- 原生查询
- 事务操作
- 监听SQL
- 存储过程
- 分布式数据库
- 第五章 服务器
- 阿里云服务器
- linux命令
- nginx
- Nginx搭建负载均衡环境简单实现
- memcached
- redis
- 杂项
- http 状态码
- 消息
- 100 Continue
- 101 Switching Protocols
- 102 Processing
- 成功
- 200 OK
- 201 Created
- 202 Accepted
- 203 Non-Authoritative Information
- 204 No Content
- 205 Reset Content
- 206 Partial Content
- 207 Multi-Status
- 重定向
- 300 Multiple Choices
- 301 Moved Permanently
- 302 Move temporarily
- 303 See Other
- 304 Not Modified
- 305 Use Proxy
- 306 Switch Proxy
- 307 Temporary Redirect
- 请求错误
- 400 Bad Request
- 401 Unauthorized
- 402 Payment Required
- 403 Forbidden
- 404 Not Found
- 405 Method Not Allowed
- 406 Not Acceptable
- 407 Proxy Authentication Required
- 408 Request Timeout
- 409 Conflict
- 410 Gone
- 411 Length Required
- 412 Precondition Failed
- 413 Request Entity Too Large
- 414 Request-URI Too Long
- 415 Unsupported Media Type
- 416 Requested Range Not Satisfiable
- 417 Expectation Failed
- 421 too many connections
- 422 Unprocessable Entity
- 423 Locked
- 424 Failed Dependency
- 425 Unordered Collection
- 426 Upgrade Required
- 449 Retry With
- 451Unavailable For Legal Reasons
- 服务器错误
- 500 Internal Server Error
- 501 Not Implemented
- 502 Bad Gateway
- 503 Service Unavailable
- 504 Gateway Timeout
- 505 HTTP Version Not Supported
- 506 Variant Also Negotiates
- 507 Insufficient Storage
- 509 Bandwidth Limit Exceeded
- 510 Not Extended
- 600 Unparseable Response Headers
- linux下自动执行任务
- restful api
- 富文本编辑器
- ueditor
- wangEditor
- js定时器
- 局域网访问本地项目
- 微信公众号网页授权登录
- 服务器配置https
- 配置虚拟目录
- ab压力测试
- 使用ab进行页面的压力测试
- apache性能测试工具ab使用详解
- 前后端分离之用户验证
- PHP面试题
- 题库一
- 题库二
- 题库三
- 题库四
- 题库五
- 题库六
- 算法题
- 星星算法
