如何在select中进行eloquent子查询select

问题描述 投票:0回答:1

我想通过这个原始查询在 Laravel Eloquent 中进行查询,我想显示 satuan 和 harga 列的乘法总和

SELECT
    rekenings.no_rekening AS no_rekening, 
    rekenings.id AS rekening_id, 
    programs.nama_program AS nama_program, 
    programs.id AS program_id, 
    detail_dpas.nama_barang AS nama_barang, 
    detail_dpas.volume AS volume,
    detail_dpas.satuan AS satuan, 
    detail_spjs.satuan AS satuanSpj, 
    detail_dpas.harga AS harga, 
    detail_spjs.harga AS hargaSpj,
    detail_spjs.catatan AS catatan, 
    detail_dpas.dpa_id AS dpa_id, 
    detail_dpas.id AS detail_dpa_id,
    (
        SELECT SUM(satuan*harga) AS totalspj
        FROM detail_spjs
        WHERE
            rekening_id = rekenings.id
            AND program_id = programs.id
    ),
    (
        SELECT SUM(volume*harga) AS totaldpa
        FROM detail_dpas
        WHERE
            detail_dpas.dpa_id = dpa_id
            AND detail_dpas.id = detail_dpa_id
    )
FROM
    spjs
    JOIN
        detail_spjs
        ON spjs.id = detail_spjs.spj_id
    JOIN
        dpas
        ON dpas.id = detail_spjs.dpa_id
    JOIN
        detail_dpas
        ON dpas.id = detail_dpas.dpa_id
    JOIN
        rekenings
        ON rekenings.id = detail_spjs.rekening_id
    JOIN
        programs
        ON programs.id = detail_spjs.program_id
WHERE
    spjs.id = $id

我已经尝试过这个,但它不起作用,

$table_spjs = DB::table('spjs')
    ->join('detail_spjs', 'spjs.id', '=', 'detail_spjs.spj_id')
    ->join('dpas', 'dpas.id', '=', 'detail_spjs.dpa_id')
    ->join('detail_dpas', 'dpas.id', '=', 'detail_dpas.dpa_id')
    ->join('rekenings', 'rekenings.id', '=', 'detail_spjs.rekening_id')
    ->join('programs', 'programs.id', '=', 'detail_spjs.program_id') 
    ->where('spjs.id', '=', $id)   
    ->select(
        'rekenings.no_rekening as no_rekening',
        'rekenings.id as rekening_id',
        'programs.nama_program as nama_program',
        'programs.id as program_id',
        'detail_dpas.nama_barang as nama_barang',
        'detail_dpas.volume as volume',
        'detail_dpas.satuan as satuan', 
        'detail_spjs.satuan as satuanSpj', 
        'detail_dpas.harga as harga',
        'detail_spjs.harga as hargaSpj',
        'detail_spjs.catatan as catatan',
        'detail_dpas.dpa_id as dpa_id',
        'detail_dpas.id as detail_dpa_id',
        DB::Raw('select sum(satuan*harga) as totalspj from detail_spjs where rekening_id=rekenings.id and program_id=programs.id'),
        DB::Raw('select sum(volume*harga) as totaldpa from detail_dpas where detail_dpas.dpa_id=dpa_id and detail_dpas.id=detail_dpa_id')
    )    
    ->get();

也许有办法在 Laravel 中使用原始查询

laravel eloquent subquery
1个回答
0
投票

我认为问题出在你的原始陈述中。

  1. 方法名称是
    raw
    ,而不是
    Raw
  2. 您缺少包裹子查询的括号。
DB::table('spjs')
   ...
   ->select(
      ...
      DB::raw('(select ... program_id = programs.id)'),
      DB::raw('(select ... detail_dpas.id = detail_dpa_id)')
   )
   ->get();

如果这不起作用,请尝试在子查询后添加别名

DB::table('spjs')
   ...
   ->select(
      ...
      DB::raw('(select ... program_id = programs.id) AS totalspj'),
      DB::raw('(select ... detail_dpas.id = detail_dpa_id) AS totaldpa')
   )
   ->get();
© www.soinside.com 2019 - 2024. All rights reserved.