[英]Yii Query Builder Where LIKE clause
我堅持下面的代碼:
->select ('t1.id_i, t2.status')
->from ('table1 as t1, table2 as t2')
->where(array('or', 'id_i'=>$model->id, array('like', 't2.status', '%Beginner%')))
這是我想要的
WHERE t1.id_i=$model->id AND/OR t2.status LIKE "Beginner" //AND/OR are optional combination
我嘗試了很多組合,但沒有結果。
請幫我。 謝謝。
我認為問題在於CDbCommand
的where()
方法不支持在 $conditions
數組中使用鍵-值對,就像在這里做的那樣: 'id_i'=>$model->id_e
。 該綁定需要通過$params
參數進行。
除此之外,我還建議您使用帶有值綁定的字符串而不是數組語法,因為這將使代碼更易於閱讀。
這是調用where()
的修改后的代碼:
->where('id_i = :id_e OR t2.status LIKE "%Beginner%"', array(':id_e' => $model->id_e))
您會看到它更接近最終的SQL,這使調試更加容易。
如果確定要使用原始語法,請嘗試以下方法:
->where(array('or', 'id_i = :id_e', array('like', 't2.status', '%Beginner%')), array(':id_e' = $model->id_e))
嘗試在CDbCommand的類上的where()方法上放置子句,where()方法指定查詢的WHERE部分,第一個argumnet是查詢條件,第二個參數是要綁定到查詢的參數(名稱=>值) 。
->select ('t1.id_i, t2.status')
->from ('table1 as t1, table2 as t2')
->where('t1.id_i=:id_i OR t2.status LIKE "%:status%"',array(':id_i' => $model->id,':status' => 'Beginner'))
有關CDbCommand where()方法的更多詳細信息,請參見此處
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.