2016-09-22 99 views
2

我有一個關於在查詢中WHERE條款雄辯的地方和與「子」或

一個問題,我想說

SELECT ... 
FROM ... 
WHERE link.hos_id = $hos_id 
AND outcome.otc_otrdischargedate = $td 
AND outcome.otc_outcome LIKE ['%ICU%', '%I.C.U%', '%L3%'] 

而是它讀起來就像這個

SELECT ... 
FROM ... 
WHERE link.hos_id = $hos_id 
AND outcome.otc_otrdischargedate = $td 
OR outcome.otc_outcome LIKE ['%ICU%', '%I.C.U%', '%L3%'] 

laravel查詢應如何結構化以像第一個例子那樣讀取?

$matchThese = ['link.hos_id' => $hos_id, 'outcome.otc_otrdischargedate' => $td]; 
    $icu = DB::table('link') 
     ->join('demographic', 'link.lnk_dmgid', '=', 'demographic.dmg_id') 
     ->join('admission', 'link.lnk_admid', '=', 'admission.adm_id') 
     ->join('outcome', 'link.lnk_otcid', '=', 'outcome.otc_id') 
     ->where($matchThese) 
     ->orWhere('outcome.otc_outcome', 'like', '%ICU%') 
     ->orWhere('outcome.otc_outcome', 'like', '%I.C.U%') 
     ->orWhere('outcome.otc_outcome', 'like', '%L3%') 
     ->orWhere('outcome.otc_outcome', 'like', '%Level3%') 
     ->orWhere('outcome.otc_outcome', 'like', '%Level 3%') 
     ->orWhere('outcome.otc_outcome', 'like', '%Intensive Care Unit%') 
     ->get(); 
    $icuSize = sizeof($icu); 

的解決方案 - 感謝@Mehravish Temkar

不同的查詢,但相同的主要

$array_conditions = array('%ICU%', '%I.C.U%','%L3%','%Level3%','%Level 3%','%Intensive Care Unit%'); 
    $step_down_icu = DB::table('link') 
     ->join('daily_link', 'link.lnk_id', '=', 'daily_link.dlk_lnkid') 
     ->join('admission', 'link.lnk_admid', '=', 'admission.adm_id') 
     ->join('outcome', 'link.lnk_otcid', '=', 'outcome.otc_id') 
     ->where('admission.adm_referraldate', '=', $td) 
     ->where('outcome.otc_outcome', 'like', '%ICU%') 
     ->Where(function ($query) use($array_conditions) { 
      for ($i = 0; $i < count($array_conditions); $i++){ 
       $query->orwhere('outcome.otc_outcome', 'like', '%' . $array_conditions[$i] .'%'); 
      } 
     })->get(); 
    $step_down_icuSize = sizeof($step_down_icu); 
+0

的多種選擇 - [ '%ICU%', '%ICU%', '%L3%'],你嘗試其中,並通過陣列代替寫單獨的還是有條件的? –

+0

替換或者在哪裏。使用哪裏爲AND和orWhere爲OR – Komal

+0

:)感謝夥計 –

回答

5

這應該工作:

$array_conditions = array('%ICU%', '%I.C.U%','%L3%'); 

$matchThese = ['link.hos_id' => $hos_id, 'outcome.otc_otrdischargedate' => $td]; 
    $icu = DB::table('link') 
     ->join('demographic', 'link.lnk_dmgid', '=', 'demographic.dmg_id') 
     ->join('admission', 'link.lnk_admid', '=', 'admission.adm_id') 
     ->join('outcome', 'link.lnk_otcid', '=', 'outcome.otc_id') 
     ->where($matchThese) 
     ->whereIn('outcome.otc_outcome', 'like', $array_conditions) 
     ->get(); 
    $icuSize = sizeof($icu); 
+0

我喜歡這個簡單的,但不幸的是它沒有工作。 – morne

+0

@morne它不顯示所需的記錄或有任何錯誤? –

+0

@morne http://stackoverflow.com/questions/34329886/laravel-querybuilder-how-to-use-like-in-wherein-function –

1

where()只需切換orWhere()。查詢生成器將鏈接SQL條款

$matchThese = ['link.hos_id' => $hos_id, 'outcome.otc_otrdischargedate' => $td]; 
$icu = DB::table('link') 
    ->join('demographic', 'link.lnk_dmgid', '=', 'demographic.dmg_id') 
    ->join('admission', 'link.lnk_admid', '=', 'admission.adm_id') 
    ->join('outcome', 'link.lnk_otcid', '=', 'outcome.otc_id') 
    ->where($matchThese) 
    ->where('outcome.otc_outcome', 'like', '%ICU%') 
    ->where('outcome.otc_outcome', 'like', '%I.C.U%') 
    ->where('outcome.otc_outcome', 'like', '%L3%') 
    ->where('outcome.otc_outcome', 'like', '%Level3%') 
    ->where('outcome.otc_outcome', 'like', '%Level 3%') 
    ->where('outcome.otc_outcome', 'like', '%Intensive Care Unit%') 
    ->get(); 
$icuSize = sizeof($icu); 
+1

um ... Obvious_Solutiuon = Face_Palm。謝謝 – morne