thinkphp如何查询数据库某一表中有多少条数据

thinkphp如何查询数据库某一表中有多少条数据,第1张

M('table')->count();

见手册统计查询:

方法说明

Count 统计数量,参数是要统计的字段名(可选)

Max 获取最大值,参数是要统计的字段名(必须)

Min 获取最小值,参数是要统计的字段名(必须)

Avg 获取平均值,参数是要统计的字段名(必须)

Sum 获取总分,参数是要统计的字段名(必须)

使用where方法

where方法支持时间比较,例如:

//

大于某个时间

where('create_time','>

time','2016-1-1');

//

小于某个时间

where('create_time','<=

time','2016-1-1');

//

时间区间查询

where('create_time','between

time',['2015-1-1','2016-1-1']);

第三个参数可以传入任何有效的时间表达式,会自动识别你的时间字段类型,支持的时间类型包括timestamps、datetime、date和int。

使用whereTime方法

whereTime方法提供了日期和时间字段的快捷查询,示例如下:

//

大于某个时间

db('user')

->whereTime('birthday',

'>=',

'1970-10-1')

->select();

//

小于某个时间

db('user')

->whereTime('birthday',

'<',

'2000-10-1')

->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();

时间表达式

还提供了更方便的时间表达式查询,例如:

//

获取今天的博客

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();

如果查询当天、本周、本月和今年的时间,还可以简化为:

//

获取今天的博客

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();

V505+版本开始,还可以使用下面的方式进行时间查询

//

查询两个小时内的博客

db('blog')

->whereTime('create_time','-2

hours')

->select();

这些在开发手册中都可以找到的。希望可以帮到你。

12$where = array('content' => array('like', '%a%'));M('xxx')->where($where)->select();

在thinkphp的查询中是找不到你这种查询方式的,一般有查询方式有:

1、$map

$map['user_id'] = array('eq', $_GET['id'];

$model->where($map)->select();

2、$where

$where['user_id'] = $_GET['id'];

$model->where($where)->select();

3、表达式

$model->where('user_id = '$_GET['id'])->select();

以上就是关于thinkphp如何查询数据库某一表中有多少条数据全部的内容,包括:thinkphp如何查询数据库某一表中有多少条数据、怎样在thinkphp 查询语句中将时间戳格式转化为年月日格式,然后再作为where条件查询、thinkphp where 查询 有if判断 怎么写在一起 。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10203107.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存