搜尋

首頁  >  問答  >  主體

以優雅的方式撰寫這個句子的技巧

我有兩個表ParametersShare_Details。我在這裡有我的原始SQL查詢,我想以純粹的Eloquent方式編寫它。請幫忙。

$shareDetails=DB::select
              ("SELECT s.id,
                       share_type,
                       para_int_1,
                       para_int_2,
                       price,
                       para_name 
             FROM share_details as s,parameters 
                  where para_type='share'
                    and para_id=share_type
                    and startDate=(select max(startdate) 
                                   from share_details 
                                   where share_type=s.share_type)
              group by share_type,
                       s.id,
                       para_int_1,
                       para_int_2,
                       price,
                       para_name");

P粉025632437P粉025632437433 天前625

全部回覆(1)我來回復

  • P粉459440991

    P粉4594409912023-09-08 17:19:47

    嘗試像這樣:

    DB::table('share_details')
    ->crossJoin('parameters'
    ->select('share_details.id', 'share_type', 'para_int_1', 'para_int_2', 'price', 'para_name')
    ->where('para_type','=','share')
    ->where('para_id','=',DB::raw('share_type'))
    ->where('startDate','=',function($query) {
        $query->from('share_details')
            ->select(DB::raw("'max'(startdate))
            ->where('share_type','=',DB::raw('s.share_type'));
    })
    ->groupBy('share_type','share_details.id','para_int_1','para_int_2','price','para_name')
    ->get();

    回覆
    0
  • 取消回覆