返回归档

Laravel

thinkphp6 where优先级查询

$map[] = [‘id’,’>‘,0]; $map[] = [‘sex’,1]; $map[] = [‘color’,‘blue’];

Db::name(‘student’) ->where($map) ->where([‘name’,‘like’,‘%efs%’]) ->select();

SELECT * FROM student WHERE id > 0 AND sex = 1 AND color = ‘blue’ AND ‘name’ LIKE ‘%efs%’;

Db::name(‘student’) ->where([$map]) ->where([‘name’,‘like’,‘%efs%’]) ->select();

SELECT * FROM student WHERE ( id > 0 AND sex = 1 AND color = ‘blue’ ) AND ‘name’ LIKE ‘%efs%’;

一般情况下,where()连缀生成的sql语句是并列AND条件的,当出现优先时,如上所示,如要优先查询$map中的条件再查询’name’条件,只需在where()中加入’[]‘即可