2017-03-20 22 views
0

我做了一個查詢誰返回我的結果也"Licencies"structure_id誰是不同於= 4。一切都很好,只是where子句,但如果我有一個orWhere的另一個條件,然後查詢顯示我也從不同的結果structure_ids ...任何人都知道正確的查詢得到只有良好的結構id與兩個where子句?感謝很多提前在一個雄辯的查詢中獲取錯誤的ID

我在這裏查詢:

$licencies = Licencies::where('structure_id' , '4')->where('statut_licence_id' , '2')->orWhere('valid_licence_id' , '1')->get(); 

這裏當我運行的代碼查詢:

select * from `licencies` where `structure_id` = '4' and `statut_licence_id` = '2' or `valid_licence_id` = '1' 

但結果我得到也從structure_id = 5 licencies對於爲例

回答

2

如果你想獲得其所有記錄structure_id 4,但與statut_licence_id「2」或valid_licence_id 1,試試這個:

$licencies = Licencies::where('structure_id' , '4') 
->where(function($query) { 
    $query->where('statut_licence_id' , '2') 
      ->orWhere('valid_licence_id' , '1') 
})->get(); 

看看參數在這裏的鏈路分組:https://laravel.com/docs/master/queries#where-clauses

+0

它現在工作!非常感謝 !! –

0

試試這個:(順便說一句,你有3個不同的id ... structure_id,statut_licence_id,valid_licence_id)確保你的邏輯正確,因爲你的查詢似乎沒問題。

$licencies = Licencies::where(['structure_id' => '4','statut_licence_id' => '2'])->orWhere('valid_licence_id' => '1')->get(); 

它的邏輯有從structure_id = 5 licencies,因爲如果valid_licence_id其1(將配合您的OR語句),那麼structure_id爲什麼不5?

+0

感謝您的回覆!它不起作用,查詢是正確的,直到orwhere子句。那麼當我把orwhere子句從其他structure_id得到所有其他的許可證 –

+0

是的,我知道,因爲你只需在你的或語句中檢查'valid_licence_id',所以'structure_id'可以是任何東西 – Onix