查詢數據庫時,有時會遇到即使用了“邏輯與”也是用了“邏輯或”的查詢條件。
他的SQL語句如下:
SELECT * FROM Persons WHERE (FirstName='Thomas' OR FirstName='William')
AND LastName='Carter'
//查詢名字爲"Thomas"或"William",並且姓氏爲"Carter"的人
在Thinkphp 中,實現這樣的查詢的方法如下
$personModel = D('person');
$where['firstName'] = array('eq','Thomas');
$where['firstName'] array('eq','William');
$where['_logic'] = "or";
$whereAll['_complex'] = $where;
$whereAll['lastname'] = "Carter";
$persons = $personModel->where($whereAll)->select();