Thinkphp field() 数据库查询函数使用
Posted 聚成网络
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Thinkphp field() 数据库查询函数使用相关的知识,希望对你有一定的参考价值。
TP 查询field小技巧
1. field 过滤掉某些字段
->where([‘id‘=>35])->field(‘delete_time‘,true)->find();
2. field 写入字段合法性检测
->field(‘title,email,content‘)->create();
3. field 操作新增字段
->field(‘id,name,price,0 as is_select‘)
4. field 运算 +-*/
->field(‘id,name,sales_origial_number+sales_actual_number as sales‘)
5. field 使用函数
->field(‘type,round(price) as price,time‘)
->field(‘type,count(id) as type_number‘)->group(‘type‘)->order(‘type_number‘)
->field(status,sum(price) as status_price)->group(status)->order(status_price)
6. field sum 统计次数
->field(‘count(id) as all_count,sum(star_rank = 1) as goods_review_count,sum(star_rank = 2) as middle_review_count,sum(star_rank = 3) as bad_review_count,sum(has_img = 2) as has_img_count‘)->where($where)->find();
7. field case when 的使用
->field(‘count(id) as all_count,IFNULL(SUM(CASE WHEN has_img = 2 THEN 1 END),0) as has_img_count,IFNULL(SUM(CASE WHEN star_rank = 1 THEN 1 END),0) as goods_review_count,IFNULL(SUM(CASE WHEN star_rank = 2 THEN 1 END),0) as middle_review_count,IFNULL(SUM(CASE WHEN star_rank = 3 THEN 1 END),0) as bad_review_count‘)->where($where)->find();
8. field 里作判断
->field("if(substr(sn,1,2)=‘SY‘,1,0) as type")
复制代码
9. field group 之后 某一个字段以“,”分割形式展示
->group(id)->field(‘group_concat(score order by `score ` desc) as score‘)复制代码
10. group 之后 field 里面查询最新一条里面的数据
->group(‘status‘)->field(‘SUBSTRING_INDEX(group_concat(id order by `id` desc),",",1) as id‘)复制代码
文章转载自 https://www.juchengvi.com/looknews/62
以上是关于Thinkphp field() 数据库查询函数使用的主要内容,如果未能解决你的问题,请参考以下文章