您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
thinkphp3.2.1学习笔记-SQL查询语句[上]
发布时间:2017-09-19 10:57:40编辑:雪饮阅读()
查询的where条件有三种
字符串形式的sql查询条件
数组索引的形式查询条件。
数组索引的形式是一种较为安全的形式,他会根据你的数组来自动为你的sql转换为完全的sql查询语句。
示例:
$user=M('user');
$condition['id']=1;
$condition['name']='xy';
$condition['_logic']='or';
$user->where($condition)->select();
其中_logic字段设置逻辑关系,如果没有该字段则默认为and
对象形式的查询条件。
$condition=new \stdClass();
$condition->id=1;
$condition->name='xy';
$condition->_logic='or';
最常用的还是数组索引形式的查询条件。下面是常见的一些查询条件。
模糊查询
$condition['name']=array('like','%x%');
模糊取反查询
$condition['name']=array('notlike','%x%');
多个模糊匹配and或or查询
$condition['name']=array('like',array('%d%','%j%'),'and');
between区间查询的两种方式:
$condition['id']=array('between',array('1','3'));
$condition['id']=array('between','1,3');
between区间取反查询:
$condition['id']=array('not between','1,3');
in区间包含查询:
$condition['id']=array('in','1,2');
in区间包含取反查询:
$condition['id']=array('not in','1,2');
exp自定义字段查询表达式查询, 比较灵活,如以下几种方式:
$condition['id']=array('exp','<1');
$condition['id']=array('exp','in (1,2,3)');
$condition['id']=array('exp','=1 and name="xy"');
关键字词:thinkphp3.2.1,sql