扫码关注官方订阅号
1 需求: 查询A表数据,同时取出B表中与A表数据Id关联的数据条数.
类似情景举例:在显示某分类文章列表时,总共有10条文章标题显示,同时在标题后面附加该文章具有多少条评论数.2 其他信息
laravel 版本5.2 php版本 5.6 mysql数据库
如何实现?
ringa_lee
select a.title,count(b.id) num from a,b where a.id=b.article_id and a.type=1 group by b.article_id;
$res = A::join('B','A.id','=','B.article_id')->selectRaw('a.title,count(b.id) num')->where('A.type',1)->groupby('B.article_id')->get();
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
select a.title,count(b.id) num from a,b where a.id=b.article_id and a.type=1 group by b.article_id;
$res = A::join('B','A.id','=','B.article_id')->selectRaw('a.title,count(b.id) num')->where('A.type',1)->groupby('B.article_id')->get();