博主信息
博文 65
粉丝 2
评论 0
访问量 74926
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
lavarel7学习笔记:聚合查询大全
张福根一修品牌运营
原创
1030人浏览过

lavarel7聚合查询

1、查询总数

//select count(*)from articles where id>5
//select count(id)from articles where id>5
$res =DB::table(‘articles’)->count();
$res =DB::table(‘articles’)->where(‘id’,’<’,5)->count();

2、统计复合条件的记录的字段的和

// select sum(pv) from articles
$res = DB::table(‘articles’)->sum(‘pv’);

3、找出复合查询条件的记录的字段的最小值

// select min(pv) from articles
// select min(pv) from articles where status=1 and pv>=16
$res = DB::table(‘articles’)->where(‘status’,1)->where(‘pv’,’>=’,16)->min(‘pv’);

4、找出复合查询条件的记录的字段的最大值

// select max(pv) from articles
$res = DB::table(‘articles’)->max(‘pv’);

5、找出复合查询条件的记录的字段的平均值

// select avg(pv) from articles
$res = DB::table(‘articles’)->avg(‘pv’);

6、查询pv大于0小于23的记录

// select from articles where status=1 and pv>0 and pv<23
// select
from articles where status=1 and pv between 0 and 23
$res = DB::table(‘articles’)->where(‘status’,1)->whereBetween(‘pv’,[0,23])->get()->all();

7、in 查询

// select from articles where pv=3 or pv=20 or pv=10
// select
from articles where pv in(3,20,10)
$res = DB::table(‘articles’)->whereIn(‘pv’,[3,20,10])->get()->all();

8、or 查询(容易全表查询,尽量不使用)

// select * from articles where pv=3 or pv=20 or pv=10
$res = DB::table(‘articles’)->orWhere(‘pv’,3)->orWhere(‘pv’,20)->orWhere(‘pv’,10)->get()->all();

9、join连表查询

// select from articles,article_cate where articles.cate_id=article_cate.cid
$res = DB::table(‘articles’)->leftJoin(‘article_cate’,’articles.id’,’=’,’article_cate.cid’)->select(‘articles.
‘,’article_cate.title as ctitle’)->get()->all();

lavarel7聚合查询

本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学