错误的方式:
$where = [];
if ($type == 'wait') {
$where['status'] = 0;
}
if ($type == 'done') {
$where['status'] = ['exp', '=1 or status = 2'];
}
$where['employee_id'] = $id;
return DB::M()
->table(self::TABLE)
->where($where)
->order('add_time desc')
->select();
SQL:
1:SELECT * FROM oa_employee_bill WHERE `status`='0' AND `employee_id`='4' ORDER BY add_time desc [Exec:0.00058984756469727s]
2:SELECT * FROM oa_employee_bill WHERE `employee_id`='4' ORDER BY add_time desc [Exec:0.00065398216247559s]
3:SELECT * FROM oa_employee_bill WHERE `status` =1 or status = 2 AND `employee_id`='4' ORDER BY add_time desc [Exec:0.00068211555480957s]
WHERE (`status` =1 )or (status = 2 AND `employee_id`='4')
正确的方式:
$where = [];
if ($type == 'wait') {
$where['status'] = 0;
}
if ($type == 'done') {
$where['status'] = ['exp', '>0 and status < 3'];
}
$where['employee_id'] = $id;
return DB::M()
->table(self::TABLE)
->where($where)
->order('add_time desc')
->select();
SQL:
1: SELECT * FROM oa_employee_bill WHERE `employee_id`='4' ORDER BY add_time desc [Exec:0.00057578086853027s]
2: SELECT * FROM oa_employee_bill WHERE `status`='0' AND `employee_id`='4' ORDER BY add_time desc [Exec:0.00056815147399902s]
3: SELECT * FROM oa_employee_bill WHERE `status` >0 and status < 3 AND `employee_id`='4' ORDER BY add_time desc [Exec:0.00062203407287598s]
WHERE `status` >0 and status < 3 AND `employee_id`='4' 只是一个情况