我有两个表galang_dana和donasi,然后尝试计算galang_dana表和donasi表中的所有数据。 galang_dana表中的数据有6个数据,当我尝试此查询时,它正确地显示了6个数据
select count(galang_dana.id_galang_dana) as jumlah_galang_dana
from galang_dana
但是当我尝试使用此查询对两个表进行内部连接以对其他数据进行计数和求和时,查询的数据选择为jumlah_galang_dana的count(galang_dana.id_galang_dana)其显示的8个数据
select count(galang_dana.id_galang_dana) as jumlah_galang_dana,
count( distinct donasi.id_user) as jumlah_donatur,
SUM(donasi.jumlah_dana) as total_dana_terkumpul
from `galang_dana` left join
`donasi`
on `galang_dana`.`id_galang_dana` = `donasi`.`id_galang_dana`
答案 0 :(得分:0)
我认为您想在加入 之前进行汇总:
select gd.jumlah_galang_dana,
d.jumlah_donatur, d.total_dana_terkumpul
from (select count(*) as jumlah_galang_dana
from `galang_dana` gd
) gd cross join
(select count(distinct d.id_user) as jumlah_donatur,
sum(d.jumlah_dana) as total_dana_terkumpul
from donasi d
) d;
答案 1 :(得分:0)
DB::table('galang_dana')
->selectRaw('
COUNT(*) jumlah_galang_dana,
(SELECT COUNT(*) FROM donasi WHERE donasi.id_galang_dana = galang_dana.id_galang_dana) jumlah_donatur,
(SELECT SUM(`jumlah_dana`) FROM donasi WHERE donasi.id_galang_dana = galang_dana.id_galang_dana) total_dana_terkumpul
')
->groupBy('id')
->get();