php框架where条件使用,where条件

>[danger] where 条件设计稍微有点复杂,所以单独拿出来

>多数情况下使用数组参数即可满足要求

>在实现复杂的where条件时,如果数组参数满足不了要求,可以使用字符串参数

>字符串参数 框架将不做解析,直接交给PDO处理

>在书写字符串参数时应尽量遵循SQL的书写规范

## 基本操作

$where['id'] = 10; // where `id`=10; 等于

$where['id >'] = 10; // where `id`>10; 大于

$where['id

$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

WHERE `id`>10 OR `pid`<20 //合并后的语句

也可以这么写:

$where['OR pid

也可以写在一个数组里面

$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函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值