2016-09-22 97 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