0
這是我目前的QB。laravel多個select語句相互覆蓋
$institutes = count($request->input("selected_institutes"));
$courses = count($request->input("selected_courses"));
$institutes_data = $request->input("selected_institutes");
$courses_data = $request->input("selected_courses");
$search=DB::table('users as u');
if($institutes > 0 && $courses = 0){
$search=$search->select('u.id','u.first_name','u.profile_picture','inst.full_name')
->leftJoin('user_institutes as ins','ins.user_id','=','u.id')
->leftJoin('institutes as inst','inst.id','=','ins.institute_id');
}
else if($courses > 0 && $institutes = 0){
$search=$search->select('u.id','u.first_name','u.profile_picture','crse.full_name')
->leftJoin('user_courses as crs','crs.user_id','=','u.id')
->leftJoin('courses as crse','crse.id','=','crs.course_id');
}
else if($institutes > 0 && $courses > 0){
$search=$search->select('u.id','u.first_name','u.profile_picture','inst.full_name','crse.full_name')
->leftJoin('user_institutes as ins','ins.user_id','=','u.id')
->leftJoin('institutes as inst','inst.id','=','ins.institute_id')
->leftJoin('user_courses as crs','crs.user_id','=','u.id')
->leftJoin('courses as crse','crse.id','=','crs.course_id');
}
$search=$search->WhereNull('u.deleted_at');
for($i=0;$i<$institutes;$i++){
$search=$search->orWhere('inst.full_name','=',"`$institutes_data[$i]`");
}
for($i=0;$i<$courses;$i++){
$search=$search->orWhere('crse.full_name','=',"`$courses_data[$i]`");
}
$search=$search->get();
結果是基於輸入,給定的情景是 $院所= 2和$課程= 2, 我打印出來 ,我希望它是落在別人第二,如果是選擇查詢示爲下面
'select * from `users` as `u` where `u`.`deleted_at` is null or `inst`.`full_name` = ?', array('`institutes 1`') or `inst`.`full_name` = ?', array('`institutes 2`')
根據這種情況的查詢我想如下
select('u.id','u.first_name','u.profile_picture','inst.full_name','crse.full_name')
leftJoin 'user_institutes as ins' on 'ins.user_id''=''u.id'
leftJoin 'institutes as inst' on 'inst.id''=''ins.institute_id'
leftJoin 'user_courses as crs' on 'crs.user_id''=''u.id'
leftJoin 'courses as crse' on 'crse.id''=''crs.course_id'
where 'u.deleted_at' is null
and ('inst.full_name' = 'institutes 1'
or 'inst.full_name' = 'institutes 2')
and ('crse.full_name' = 'courses 1'
or 'crse.full_name' = 'courses 2'}
任何人都可以執行克
請使用這些if else條件構造我的QB併爲where子句變量輸入運行for循環。 (我的語法道歉)
什麼被覆蓋? –
也許你應該只添加另一個如果$ institutes = 2和$ courses = 2的具體數量。 – Grynets
@ jedrzej.kurylo,select語句被覆蓋,但我試圖修改QB,這是最新的輸出只是不如我在尋找 – kearn