如何将SQL查询转换为Laravel数据库查询?

时间:2017-05-09 07:07:52

标签: database laravel

我有一个SQL查询。

SELECT
    nguyenlieu.manguyenlieu,
    nguyenlieu.tennguyenlieu,
    SUM(chitietnhapmua.soluong) AS slsum
FROM chitietnhapmua
JOIN nguyenlieu
    ON  nguyenlieu.manguyenlieu = chitietnhapmua.manguyenlieu
JOIN
(SELECT nhapmua.* FROM nhapmua WHERE nhapmua.trangthai = 1) AS nhapmua
    ON nhapmua.maphieunhap = chitietnhapmua.maphieunhap
GROUP BY nguyenlieu.manguyenlieu

它在SQL上运行。我得试试

$data = DB::table('nguyenlieu')
    ->join('chitietnhapmua','nguyenlieu.manguyenlieu','=','chitietnhapmua.manguyenlieu')
          ->join('nhapmua',function($join){
              $join->on('nhapmua.maphieu','=','chitietnhapmua.maphieu')
                  ->where('nhapmua.trangthai','=','1');
          })
          ->select('nhapmua.maphieunhap','nguyenlieu.manguyenlieu','nguyenlieu.tennguyenlieu','chitietnhapmua.soluong')
          ->get();

但我无法使用类DB转换为Laravel框架。

0 个答案:

没有答案