TP5 where多条件查询

Where 条件表达式格式为:

$where[‘字段名’] = array(‘表达式’, ‘操作条件’);

$res = Db::name(‘tablename’)->where($where)->select();

| SQL运算符 | sql运算符 | 例子 |实际查询条件

TP运算符 SQL运算符 例子 实际查询条件
eq = $where[‘id’] = array(‘eq’,100); 等效于:$map[‘id’] = 100;
neq != $where[‘id’] = array(‘neq’,100); id != 100
gt > $where[‘id’] = array(‘gt’,100); id > 100
egt >= $where[‘id’] = array(‘egt’,100); id >= 100
lt < $where[‘id’] = array(‘lt’,100); id < 100
elt <= $where[‘id’] = array(‘elt’,100); id <= 100
like like $where(‘username’) = array(‘like’,’%admin%’); username like ‘%admin%’
between between and $where[‘id’] = array(‘between’,‘1,8’); id BETWEEN 1 AND 8
not between not between and $where[‘id’] = array(‘not between’,‘1,8’); id NOT BETWEEN 1 AND 8
in in $where[‘id’] = array(‘in’,‘1,5,8’); id in(1,5,8)
not in not in $where[‘id’] = array(‘not in’,‘1,5,8’); id not in(1,5,8)
and(默认) and $where[‘id’] = array(array(‘gt’,1),array(‘lt’,10)); (id > 1) AND (id < 10)
or or $where[‘id’] = array(array(‘gt’,3),array(‘lt’,10), ‘or’); (id > 3) OR (id < 10)
xor(异或) xor 两个输入中只有一个是true时,结果为true,否则为false,例子略。 1 xor 1 = 0
exp 综合表达式 $where[‘id’] = array(‘exp’,‘in(1,3,8)’); $where[‘id’] = array(‘in’,‘1,3,8’);

除此之外还有多个字段的联合查询

TP运算 SQL运算 说明
$where[‘user1&user2&user3’] = [‘eq’, ‘openid’]; “user1= openid and user2= openid and user3= openid” 多个字段相同值的and查询
$where[‘user1|user2|user3’] = [‘eq’, ‘openid’]; “user1= openid or user2= openid or user3= openid” 多个字段相同值的或查询

你可能感兴趣的:(MySQL,thinkphp5)