gpt4 book ai didi

php - 我如何使用 laravel 获得多个计数查询?

转载 作者:行者123 更新时间:2023-11-29 21:39:49 26 4
gpt4 key购买 nike

我需要一些查询生成器来获得如下结果:

select 
A.`idalbum`, A.`title_en` as `title`,
A.`desc_en` as `description`, A.`cover` as `cover`,
(SELECT COUNT(idgallery) AS gambar FROM gallery WHERE cate='1' AND albumid=A.idalbum) as gambar,
(SELECT COUNT(idgallery) AS videos FROM gallery WHERE cate='2' AND albumid=A.idalbum) as videos,
from `album` as A

我应该使用什么查询?万一我不能使用 RAW,因为我需要在 laravel Eloquent 上进行一些分页

最佳答案

我从另一个回复中得到了这个答案:

$result = DB::table('album as A')
->select("A.idalbum", "A.title_en as title",
"A.desc_en as description", "A.cover as cover",
DB::raw("(SELECT COUNT(idgallery) AS gambar FROM gallery WHERE cate='1' AND albumid=A.idalbum) as gambar"),
DB::raw("(SELECT COUNT(idgallery) AS videos FROM gallery WHERE cate='2' AND albumid=A.idalbum) as videos")
)
->orderBy('A.date', 'asc')->paginate($this->perpage);


return $result;

我应该删除这个问题吗?

关于php - 我如何使用 laravel 获得多个计数查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34628287/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com