2016-05-11 14 views
2

mysql文をlaravelクエリービルダーに変換しようとしています。私はの結果からフェッチされた列にGROUP BYとORDER BYを実行しようとしてい次のクエリでは3つのテーブルlaravel query builderを使用してテーブルの結合結果から列を選択する方法を教えてください。

  • staff_task_history_calibrate
  • staff_task_history_samples
  • staff_task_history_measures

をUNION-INGのあなたはそれに似た何かを見つけましたか?はいの場合は、私の問題についていくつか明記してください。

私がテストしたが、試してみるための
SELECT staff_name, task_id, task_type, task_desc, SUM(task_multiplier) 
FROM 
(
    (
    select CONCAT(S.first_name," ",S.last_name) as staff_name, 
    `STHC`.`task_id`, IFNULL(T.type, "-") as task_type, 
    `T`.`description` as `task_desc`, 
    STHC.task_multiplier 
    from `staff_task_history_calibrate` as `STHC` 
    inner join `staffs` as `S` on `STHC`.`staff_id` = `S`.`staff_id` inner join `tasks` as `T` on `STHC`.`task_id` = `T`.`id` 
    ) 
    union 
    (
    select CONCAT(S.first_name," ",S.last_name) as staff_name, 
    `STHS`.`task_id`, IFNULL(T.type, "-") as task_type, 
    `T`.`description` as `task_desc`, 
    STHS.task_multiplier 
    from `staff_task_history_samples` as `STHS` 
    inner join `staffs` as `S` on `STHS`.`staff_id` = `S`.`staff_id` inner join `tasks` as `T` on `STHS`.`task_id` = `T`.`id` 
    ) 
    union 
    (
    select CONCAT(S.first_name," ",S.last_name) as staff_name, 
    `STHM`.`task_id`, IFNULL(T.type, "-") as task_type, 
    `T`.`description` as `task_desc`, 
    STHM.task_multiplier 
    from `staff_task_history_measures` as `STHM` 
    inner join `staffs` as `S` on `STHM`.`staff_id` = `S`.`staff_id` inner join `tasks` as `T` on `STHM`.`task_id` = `T`.`id` 
    ) 
) combined_tables 
GROUP BY staff_name, task_type, task_desc 
ORDER BY staff_name, task_type, task_desc; 

答えて

3

難しい:私は `mergeBindings`を使用する理由を理解しない

// Build up the sub-queries 
$sub1 = DB::table('staff_task_history_calibrate AS STHC') 
      ->select(DB::raw(
      'CONCAT(S.first_name," ",S.last_name) as staff_name', 
      'STHC'.'task_id', 'IFNULL(T.type, "-") as task_type', 
      'T'.'description' as 'task_desc', 'STHC.task_multiplier') 
     )->join('staffs AS S', 'STHC.staff_id', '=', 'S.staff_id') 
      ->join('tasks AS T', 'STHC.task_id', '=', 'T.id'); 

$sub2 = DB::table('staff_task_history_samples AS STHS') 
      ->select(DB::raw(
      'CONCAT(S.first_name," ",S.last_name) as staff_name', 
      'STHS'.'task_id', 'IFNULL(T.type, "-") as task_type', 
      'T'.'description' as 'task_desc', 'STHS.task_multiplier') 
     )->join('staffs AS S', 'STHS.staff_id', '=', 'S.staff_id') 
      ->join('tasks AS T', 'STHS.task_id', '=', 'T.id'); 

$sub3 = DB::table('staff_task_history_measures AS STHM') 
      ->select(DB::raw(
      'CONCAT(S.first_name," ",S.last_name) as staff_name', 
      'STHM'.'task_id', 'IFNULL(T.type, "-") as task_type', 
      'T'.'description' as 'task_desc', 'STHM.task_multiplier') 
     )->join('staffs AS S', 'STHM.staff_id', '=', 'S.staff_id') 
      ->join('tasks AS T', 'STHM.task_id', '=', 'T.id'); 


// Add the unions 
$allUnions = $sub1->union($sub2)->union($sub3); // (Check the documentation for Unions in Query Builder) 

// Get the results 
$results = DB::table(DB::raw("({$allUnions->toSql()}) as combined_tables")) 
     ->select('staff_name, task_id, task_type, task_desc') 
     ->sum('task_multiplier') 
     ->mergeBindings($allUnions) // We need to retrieve the underlying SQL 
     ->groupBy('staff_name') 
     ->groupBy('task_type') 
     ->groupBy('task_desc') 
     ->orderBy('staff_name') 
     ->orderBy('task_type') 
     ->orderBy('task_desc') 
     ->get(); 

(あなたが作品を持っているものならば、私はそれを維持したい。)

+0

。この方法についてもっと詳しく説明してください。 – alex

関連する問題