为什么我在 laravel 查询生成器中得到不同的结果
Why I i got different result in laravel query builder
所以我尝试将此 sql 查询转换为 laravel 查询构建器
select jenis_data.id_jenisdata, jenis_data.id_level, jenis_data.nama_data, detail_A.id_kecamatan, satuan.satuan,
detail_A.tahun, detail_A.angka as tahun_2018, detail_B.angka as tahun_2019, opd.id_opd, detail_A.id_bidang, detail_A.id_detaildata,
opd.wali_data, jenis_data.perkecamatan from jenis_data
left join detail_data as detail_A on jenis_data.id_jenisdata = detail_A.id_jenisdata
left join opd on detail_A.id_opd = opd.id_opd
left join satuan on jenis_data.id_satuan = satuan.id_satuan
left join detail_data as detail_B on detail_A.id_jenisdata = detail_B.id_jenisdata
AND detail_A.tahun = 2018
AND detail_B.tahun = 2019
group by jenis_data.id_jenisdata, detail_A.id_kecamatan
我想出了这个查询生成器
DB::table('jenis_data')
->select('jenis_data.id_jenisdata','jenis_data.id_level','jenis_data.nama_data','detail_A.id_kecamatan','satuan.satuan',
'detail_A.tahun','detail_A.angka as tahun_2018', 'detail_B.angka as tahun_2019', 'opd.id_opd', 'detail_A.id_bidang',
'opd.wali_data','jenis_data.perkecamatan')
->leftJoin('detail_data as detail_A', 'jenis_data.id_jenisdata','=','detail_A.id_jenisdata')
->leftJoin('opd','detail_A.id_opd','=','opd.id_opd')
->leftJoin('satuan','jenis_data.id_satuan','=','satuan.id_satuan')
->leftJoin('detail_data as detail_B', 'detail_A.id_jenisdata', '=', 'detail_B.id_jenisdata')
->where('detail_A.tahun','2018')
->where('detail_B.tahun','2019')
->groupBy('jenis_data.id_jenisdata', 'detail_A.id_kecamatan')
->get();
问题是我得到了不同的结果,laravel 查询生成器的结果少于 sql 查询。就像我在使用查询构建器进行内连接而不是左连接。
result from sql query
result from query builder
在您的第一个查询中,没有“where”子句。
在查询生成器中...
->leftJoin('detail_data as detail_B', function ($join) {
$join->on('detail_A.id_jenisdata', '=', 'detail_B.id_jenisdata')
->where('detail_A.tahun','2018')
->where('detail_B.tahun','2019');
})
所以我尝试将此 sql 查询转换为 laravel 查询构建器
select jenis_data.id_jenisdata, jenis_data.id_level, jenis_data.nama_data, detail_A.id_kecamatan, satuan.satuan,
detail_A.tahun, detail_A.angka as tahun_2018, detail_B.angka as tahun_2019, opd.id_opd, detail_A.id_bidang, detail_A.id_detaildata,
opd.wali_data, jenis_data.perkecamatan from jenis_data
left join detail_data as detail_A on jenis_data.id_jenisdata = detail_A.id_jenisdata
left join opd on detail_A.id_opd = opd.id_opd
left join satuan on jenis_data.id_satuan = satuan.id_satuan
left join detail_data as detail_B on detail_A.id_jenisdata = detail_B.id_jenisdata
AND detail_A.tahun = 2018
AND detail_B.tahun = 2019
group by jenis_data.id_jenisdata, detail_A.id_kecamatan
我想出了这个查询生成器
DB::table('jenis_data')
->select('jenis_data.id_jenisdata','jenis_data.id_level','jenis_data.nama_data','detail_A.id_kecamatan','satuan.satuan',
'detail_A.tahun','detail_A.angka as tahun_2018', 'detail_B.angka as tahun_2019', 'opd.id_opd', 'detail_A.id_bidang',
'opd.wali_data','jenis_data.perkecamatan')
->leftJoin('detail_data as detail_A', 'jenis_data.id_jenisdata','=','detail_A.id_jenisdata')
->leftJoin('opd','detail_A.id_opd','=','opd.id_opd')
->leftJoin('satuan','jenis_data.id_satuan','=','satuan.id_satuan')
->leftJoin('detail_data as detail_B', 'detail_A.id_jenisdata', '=', 'detail_B.id_jenisdata')
->where('detail_A.tahun','2018')
->where('detail_B.tahun','2019')
->groupBy('jenis_data.id_jenisdata', 'detail_A.id_kecamatan')
->get();
问题是我得到了不同的结果,laravel 查询生成器的结果少于 sql 查询。就像我在使用查询构建器进行内连接而不是左连接。
result from sql query
result from query builder
在您的第一个查询中,没有“where”子句。
在查询生成器中...
->leftJoin('detail_data as detail_B', function ($join) {
$join->on('detail_A.id_jenisdata', '=', 'detail_B.id_jenisdata')
->where('detail_A.tahun','2018')
->where('detail_B.tahun','2019');
})