在Laravel 5.3上左连接查询

时间:2017-05-17 00:27:20

标签: mysql laravel

我对mySQL有这样的查询

select p.tgl_pesan, COALESCE(SUM(dp.jumlah), 0) as jumlah 
FROM pemesanan p 
LEFT JOIN detail_pemesanan dp ON p.id_pemesanan = dp.id_pemesanan 
WHERE p.tgl_pesan >= DATE_ADD(NOW(), INTERVAL -6 day) 
GROUP BY p.tgl_pesan

我尝试将该查询应用于我的Laravel项目

$data = DB::table('detail_pemesanan')
      ->leftJoin('pemesanan', 'detail_pemesanan.id_pemesanan', '=', 'pemesanan.id_pemesanan')
      ->select('pemesanan.tgl_pesan as tgl_pesan', DB::raw('SUM(detail_pemesanan.jumlah) as jumlah'))
      ->where('tgl_pesan', '>=', 'DATE_ADD(NOW(), INTERVAL -6 DAY)')
      ->groupBy('pemesanan.tgl_pesan')
      ->get();

但两者都显示出不同的结果。我的Laravel项目的结果显示了像mySQL这样的意外数据,它显示了来自我的间隔的更多数据。我对Laravel项目的查询有什么问题吗?非常感谢你

1 个答案:

答案 0 :(得分:0)

你的SQL查询将是:

def destroy
  @masters = {}
  @subdoc = Subdoc.find(params[:id])
  @masters = current_user.user.masters

  # Tried these methods to delete the value.
  # @subdocs = @masters.select { |t| t.subdocs.to_a - [params[:id]] }
  # @subdocs = @masters.each { |t| t[:subdocs] - params[:id] }   

  @subdoc.destroy
  return render :status => 200, :json => { :success => true }    
end