where条件怎么写
说明
where方法可以多次调用
一、根据主键查询
$db->table('user')->where(100)->row(); //等价于where('id', 100)
// SELECT * FROM `user` WHERE `id` = 100
二、查询表达式
$db->table('user')
->fields('id, nickname, avatar')
->where('grade', '>', 4)
->where('status', 1) //等于可以简写成这种
->select();
// SELECT id, nickname, avatar FROM `user` WHERE `grade` > 4 AND `status` = 1
更多查询表达式
=、>、<、<>、>=、<=
between、in、like、find_in_set
where('grade', 'between', [3, 5])
where('id', 'in', [1,3,4,6,8])
where('nickname', 'like', '%timo%')
$db->table('user')->where('pids', 'find_in_set', 10)->select();
三、索引数组
$db->table('user')
->where([
['grade', 'between', [3,5]],
['status', 1]
])->select();
// SELECT * FROM `user` WHERE `grade` BETWEEN 3 AND 5 AND `status` = 1
四、关联数组
$db->table('user')
->where(['grade' => ['between', 3, 5], 'status' => 1])
->select();
// SELECT * FROM `user` WHERE `grade` BETWEEN 3 AND 5 AND `status` = 1
五、or条件查询
我们不推荐在项目中使用or查询
简单
$user = $db->table('user')
->where('sex', 2)
->orWhere('sex', 0)
->select();
var_dump($db->getLastSql());
// SELECT * FROM `user` WHERE `sex` = 2 OR `sex` = 0
复杂
$user = $db->table('user')
->where('sex', 2)
->orWhere(function () {
return [
'sex' => 0,
'status' => 1
];
})
->select();
var_dump($db->getLastSql());
// SELECT * FROM `user` WHERE `sex` = 2 OR (`sex` = 0 AND `status` = 1)