如何将mysql查询转换为laravel查询

n9vozmp4  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(474)

我在mysql中的所有原始查询都需要转换为laravel 5.4查询生成器:

SELECT MONTH(calendar.datefield) AS Bulan, calendar.datefield AS Tanggal, IFNULL(SUM(dbk.jumlah),0) AS Jumlah 
FROM detail_brg_keluar AS dbk 
INNER JOIN penjualan AS pen ON pen.id = dbk.id_brg_keluar 
RIGHT JOIN calendar ON (DATE(pen.tgl_keluar) = calendar.datefield)
WHERE 
(calendar.datefield BETWEEN 
 (SELECT MIN(DATE(tgl_keluar)) FROM penjualan AS p
    INNER JOIN detail_brg_keluar AS detail ON p.id = detail.id_brg_keluar 
    WHERE detail.id_produk = 818002) 
 AND 
 (SELECT MAX(DATE(tgl_keluar)) FROM penjualan AS pe
    INNER JOIN detail_brg_keluar AS det ON pe.id = det.id_brg_keluar
    WHERE det.id_produk = 818002))
AND (dbk.id_produk = 818002 OR dbk.id_produk IS NULL)
GROUP BY MONTH(calendar.datefield)
ORDER BY calendar.datefield ASC


这是我的laravel代码,代码错误-_-

$proT = DB::table('detail_brg_keluar AS dbk')
    ->select( DB::raw('MONTH(calendar.datefield) AS Bulan, calendar.datefield AS Tanggal, IFNULL(SUM(dbk.jumlah),0) AS Jumlah '))
    ->join('penjualan AS pen', 'pen.id', '=', 'dbk.id_brg_keluar')
    ->join('calendar', DB::raw('DATE(pen.tgl_keluar)'), '=', 'calendar.datefield')
    ->whereBetween('calendar.datefield', [$from[0]->tanggalAwal, $to[0]->tanggalAkhir])
    ->where(DB::raw('dbk.id_produk = 818002 OR dbk.id_produk IS NULL'))
    ->groupBy(DB::raw('MONTH(calendar.datefield)'))
    ->orderBy(DB::raw('calendar.datefield', 'ASC'))
    ->get();

代码从和到:

$from = DB::table('penjualan AS p')
    ->select(DB::raw('MIN(DATE(tgl_keluar)) AS tanggalAwal'))
    ->join('detail_brg_keluar AS detail', 'p.id', '=', 'detail.id_brg_keluar')
    ->where('detail.id_produk', $id)
    ->get();

$to = DB::table('penjualan AS p')
    ->select(DB::raw('MAX(DATE(tgl_keluar)) AS tanggalAkhir'))
    ->join('detail_brg_keluar AS detail', 'p.id', '=', 'detail.id_brg_keluar')
    ->where('detail.id_produk', $id)
    ->get();

我不知道了?
全部thx

qltillow

qltillow1#

使用此选项:

$from = DB::table('penjualan AS p')
    ->select(DB::raw('MIN(DATE(tgl_keluar)) AS tanggalAwal'))
    ->join('detail_brg_keluar AS detail', 'p.id', '=', 'detail.id_brg_keluar')
    ->where('detail.id_produk', $id);
$to = DB::table('penjualan AS p')
    ->select(DB::raw('MAX(DATE(tgl_keluar)) AS tanggalAkhir'))
    ->join('detail_brg_keluar AS detail', 'p.id', '=', 'detail.id_brg_keluar')
    ->where('detail.id_produk', $id);
$proT = DB::table('detail_brg_keluar AS dbk')
    ->select( DB::raw('MONTH(calendar.datefield) AS Bulan, calendar.datefield AS Tanggal, IFNULL(SUM(dbk.jumlah),0) AS Jumlah '))
    ->join('penjualan AS pen', 'pen.id', '=', 'dbk.id_brg_keluar')
    ->rightJoin('calendar', DB::raw('DATE(pen.tgl_keluar)'), '=', 'calendar.datefield')
    ->whereRaw('calendar.datefield BETWEEN ('.$from->toSql().') AND ('.$to->toSql().')', [$id, $id])
    ->where(function($query) {
        $query->where('dbk.id_produk', 818002)->orWhereNull('dbk.id_produk');
    })
    ->groupBy(DB::raw('MONTH(calendar.datefield)'))
    ->orderBy('calendar.datefield', 'ASC')
    ->get();

不执行 $from 以及 $to ,使用生成的sql。
我替换了 ->join('calendar'->rightJoin('calendar' .
我替换了 where(DB::raw()) 使用嵌套约束。 orderBy() 不需要原始的表达。

相关问题