The following tutorial column will introduce you to the aggregation query, time query, and advanced query of ThinkPHP database operations. I hope it will be helpful to friends in need! Aggregation query
In applications, we often use some statistical data, such as all current users (or those who meet certain conditions) number, the maximum points of all users, the average score of users, etc. ThinkPHP provides a series of built-in methods for these statistical operations, including:
Usage example: Get the number of users:
Db::table('think_user')->count(); // 助手函数 db('user')->count();
Or according to field statistics:
Db::table('think_user')->count('id'); // 助手函数 db('user')->count('id');
Get the maximum points of users:
Db::table('think_user')->max('score'); // 助手函数 db('user')->max('score');
Get the minimum points of users whose points are greater than 0:
Db::table('think_user')->where('score>0')->min('score'); // 助手函数 db('user')->where('score>0')->min('score');
Get the user’s average points:
Db::table('think_user')->avg('score'); // 助手函数 db('user')->avg('score');
Statistics on the user’s total score:
Db::table('think_user')->sum('score'); // 助手函数 db('user')->sum('score');Time query
Time comparison
##Use where method where
The method supports time comparison, for example: // 大于某个时间
where('create_time','> time','2016-1-1');
// 小于某个时间
where('create_time','
The third parameter can be passed in any valid time expression, and your time field type will be automatically recognized. Supported time types include timestamps, datetime, date and int.
Use the whereTime method
The whereTime method provides quick query for date and time fields, the example is as follows: // 大于某个时间
db('user') ->whereTime('birthday', '>=', '1970-10-1') ->select();
// 小于某个时间
db('user') ->whereTime('birthday', 'select();
// 时间区间查询
db('user') ->whereTime('birthday', 'between', ['1970-10-1', '2000-10-1']) ->select();
// 不在某个时间区间
db('user') ->whereTime('birthday', 'not between', ['1970-10-1', '2000-10-1']) ->select();
Time expression
also provides more convenient time expression query, for example: // 获取今天的博客
db('blog') ->whereTime('create_time', 'today') ->select();
// 获取昨天的博客
db('blog') ->whereTime('create_time', 'yesterday') ->select();
// 获取本周的博客
db('blog') ->whereTime('create_time', 'week') ->select();
// 获取上周的博客
db('blog') ->whereTime('create_time', 'last week') ->select();
// 获取本月的博客
db('blog') ->whereTime('create_time', 'month') ->select();
// 获取上月的博客
db('blog') ->whereTime('create_time', 'last month') ->select();
// 获取今年的博客
db('blog') ->whereTime('create_time', 'year') ->select();
// 获取去年的博客
db('blog') ->whereTime('create_time', 'last year') ->select();
If you query the time of the day, this week, this month and this year, it can also be simplified to:
// 获取今天的博客 db('blog') ->whereTime('create_time', 'd') ->select(); // 获取本周的博客 db('blog') ->whereTime('create_time', 'w') ->select(); // 获取本月的博客 db('blog') ->whereTime('create_time', 'm') ->select(); // 获取今年的博客 db('blog') ->whereTime('create_time', 'y') ->select();Starting from version V5.0.5, you can also use the following method to query the time
// 查询两个小时内的博客 db('blog') ->whereTime('create_time','2 hours') ->select();
Advanced query
Quick query
Quick query method isA simplified way of writing the same query conditions in multiple fields
, which can further simplify the writing of query conditions. Use | to separate multiple fields to represent OR queries. Use & to separate AND query, you can implement the following query, for example:Db::table('think_user') ->where('name|title','like','thinkphp%') ->where('create_time&update_time','>',0) ->find();The generated query SQL is:
SELECT * FROM `think_user` WHERE ( `name` LIKE 'thinkphp%' OR `title` LIKE 'thinkphp%') AND ( `create_time` > 0 AND `update_time` > 0 ) LIMIT 1Quick query supports all query expressions.
Interval query
Interval query isA kind of multiple queries for the same field Simplified way of writing the query condition
, for example:Db::table('think_user') ->where('name',['like','thinkphp%'],['like','%thinkphp']) ->where('id',['>',0],['',10],'or') ->find();The generated SQL statement is:
SELECT * FROM `think_user` WHERE ( `name` LIKE 'thinkphp%' AND `name` LIKE '%thinkphp') AND ( `id` > 0 OR `id` 10 ) LIMIT 1The query condition of the interval query must be defined in an array, and all query expression.
The following query method is wrong:
Db::table('think_user') ->where('name',['like','thinkphp%'],['like','%thinkphp']) ->where('id',5,['',10],'or') ->find();
Batch query
You can define batch conditional queries with multiple conditions, for example: Db::table('think_user'->'name' => ['like','thinkphp%'],
'title' => ['like','%thinkphp'],
'id' => ['>',0],
'status'=> 1->
The generated SQL statement is:
SELECT * FROM `think_user` WHERE `name` LIKE 'thinkphp%' AND `title` LIKE '%thinkphp' AND `id` > 0 AND `status` = '1'
Closure Query
Db::table('think_user')->select(function($query){ $query->where('name','thinkphp') ->whereOr('id','>',10);
});
The generated SQL statement is: SELECT * FROM `think_user` WHERE `name` = 'thinkphp' OR `id` > 10
Use Query object to query
$query = new \think\db\Query;$query->name('user') ->where('name','like','%think%') ->where('id','>',10) ->limit(10); Db::select($query);
If a Query object is used, any chain operations called before the select method will be invalid.
Mixed query
You can combine all the methods mentioned above to perform mixed query, for example:Db::table('think_user') ->where('name',['like','thinkphp%'],['like','%thinkphp']) ->where(function($query){ $query->where('id',['',100],'or');
}) ->select();
The generated SQL statement is:
SELECT * FROM `think_user` WHERE ( `name` LIKE 'thinkphp%' AND `name` LIKE '%thinkphp') AND ( `id` 100 )
String condition query
For some practical Complex queries can also be queried directly using native SQL statements, for example: Db::table('think_user') ->where('id > 0 AND name LIKE "thinkphp%"') ->select();
For safety reasons, we can use parameter binding for string query conditions, for example:
Db::table('think_user') ->where('id > :id AND name LIKE :name ',['id'=>0, 'name'=>'thinkphp%']) ->select();V5. Starting from 0.4, ThinkPHP supports calling query conditions multiple times on the same field, for example:
Db::table('think_user') ->where('name','like','%think%') ->where('name','like','%php%') ->where('id','in',[1,5,80,50]) ->where('id','>',10) ->find();Shortcut method (V5.0.5)
V5.0.5 version has added a series of shortcut methods to simplify queries, including:
The above is the detailed content of ThinkPHP database operation aggregation query, time query, advanced query. For more information, please follow other related articles on the PHP Chinese website!

thinkphp是国产框架。ThinkPHP是一个快速、兼容而且简单的轻量级国产PHP开发框架,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了关于使用think-queue来实现普通队列和延迟队列的相关内容,think-queue是thinkphp官方提供的一个消息队列服务,下面一起来看一下,希望对大家有帮助。

thinkphp基于的mvc分别是指:1、m是model的缩写,表示模型,用于数据处理;2、v是view的缩写,表示视图,由View类和模板文件组成;3、c是controller的缩写,表示控制器,用于逻辑处理。mvc设计模式是一种编程思想,是一种将应用程序的逻辑层和表现层进行分离的方法。

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了使用jwt认证的问题,下面一起来看一下,希望对大家有帮助。

thinkphp扩展有:1、think-migration,是一种数据库迁移工具;2、think-orm,是一种ORM类库扩展;3、think-oracle,是一种Oracle驱动扩展;4、think-mongo,一种MongoDb扩展;5、think-soar,一种SQL语句优化扩展;6、porter,一种数据库管理工具;7、tp-jwt-auth,一个jwt身份验证扩展包。

thinkphp查询库是否存在的方法:1、打开相应的tp文件;2、通过“ $isTable=db()->query('SHOW TABLES LIKE '."'".$data['table_name']."'");if($isTable){...}else{...}”方式验证表是否存在即可。

本篇文章给大家带来了关于ThinkPHP的相关知识,其中主要整理了使用think-queue实现redis消息队列的相关问题,下面一起来看一下,希望对大家有帮助。

在thinkphp3.2中,可以利用define关闭调试模式,该标签用于变量和常量的定义,将入口文件中定义调试模式设为FALSE即可,语法为“define('APP_DEBUG', false);”;开启调试模式将参数值设置为true即可。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
