用AI赚第一桶💰低成本搭建一套AI赚钱工具,源码可二开。 广告
>[danger] where 条件设计稍微有点复杂,所以单独拿出来 >多数情况下使用数组参数即可满足要求 >在实现复杂的where条件时,如果数组参数满足不了要求,可以使用字符串参数 >字符串参数 框架将不做解析,直接交给PDO处理 >在书写字符串参数时应尽量遵循SQL的书写规范 ## 基本操作 $where['id'] = 10; // where `id`=10; 等于 $where['id >'] = 10; // where `id`>10; 大于 $where['id <'] = 10; //where `id`<10; 小于 $where['id >='] = 10; //where `id`>=10; 大于等于 $where['id <='] = 10; //where `id`<=10; 小于等于 $where['id <>'] = 10; //where `id`<>10; 不等于 **IN 和 NOT IN** $where['id'] = [2,4,6,8]; //where `id` IN(2,4,6,8); $where['id'] = ['IN',[2,4,6,8]]; //同上 指定操作符是'IN' $where['id'] = ['NOT IN',[2,4,6,8]]; //where `id` NOT IN(2,4,6,8) **BETWEEN 和 NOT BETWEEN** $where['id'] = ['BETWEEN',[1,10]] //WHERE `id` BETWEEN 1 AND 10 $where['id'] = ['NOT BETWEEN',[1,10]] //WHERE `id` NOT BETWEEN 1 AND 10 **LIKE** $where['name'] = ['LIKE','tom'] //WHERE `name` LIKE %tom% $where['name'] = '%tom%' //WHERE `name` LIKE %tom% **多个字段对应同一个值的情况:** $where['id|uid'] = 10; //where `id`=10 OR `uid`=10 ## 连接多个条件 **默认使用 AND 连接多个条件** $where['id >'] = 10; $where['pot >'] = 100; //...更多 where `id`>10 AND `pot`>100 AND ...更多 //合并后的条件 **OR** $where['id >'] = 10; $where['pid <'] = ['OR',20]; WHERE `id`>10 OR `pid`<20 //合并后的语句 也可以这么写: $where['OR pid <'] = 20; 也可以写在一个数组里面 $where = ['id >'=>10,'OR pid <'=>20]; >[danger]where条件可以多次调用以应对稍微复杂一点的条件 $m = D('user'); $where1 = ['id >'=>10,'OR pot >'=>100]; $where2 = ['OR pid <'=> 20]; $user = $m->where($where1)->where($where2)->select(); //两次调用where()函数合并之后的 where 条件: WHERE (`id `>10 OR `pot `>100) OR (`pid`<20) ## 使用字符串参数 **框架不做解析,也不绑定参数,需注意语句安全** $where = "`id`>10 AND `pot`>100"; **绑定参数的方式:** $m = D('user'); $where = "`id`>:id AND `pot`>:pot"; //将参数绑定到 :id 和 :pot 上 $arr = [':id'=>10,':pot'=>100]; //给绑定参数赋值 $user = $m->where($where,$arr)->select(); //$arr作为第二个参数传入where函数