Thinkphp 逻辑与,逻辑或的复合查询

查询数据库时,有时会遇到即使用了“逻辑与”也是用了“逻辑或”的查询条件。
他的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();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章