发布日期: 2021-09-29 | 来源: 智软设计工作室
有查询条件就查询,
多个查询条件,只要有查询,就增加一个查询条件
//类型
if($sotype){
$where['type'] = $sotype;
}
//合作单位
if($companyid){
$where['hezuodanwei'] = $companyid;
}
//关键词 模糊查询 $type 是变量
if($key){
$where[$type] = ['like',"%".$key."%"];
}
$rs=Db::name('student')->where($where)->order('id desc')->limit($limit)->page($page)->select();
$rs1=Db::name('student')->where($where)->select();
$where['type'] = $sotype;
$where['hezuodanwei'] = $companyid;
$where["username"] = ['like',"%".$tag["kw"]."%"];//模糊查询
$where[]=['exp','FIND_IN_SET(2,needID)'];
例子:id in(1,5,8)
$where['hezuodanwei'] =array('in','10,12');
组成查询数组$where
where($where)
引用:http://blog.csdn.net/u010447573/article/details/47420063
Where 条件表达式格式为:
$map['字段名'] = array('表达式', '操作条件');其中 $map 是一个普通的数组变量,可以根据自己需求而命名。上述格式中的表达式实际是运算符的意义:
| TP运算符 | SQL运算符 | 例子 | 实际查询条件 |
|---|---|---|---|
| eq | = | $map['id'] = array('eq',100); | 等效于:$map['id'] = 100; |
| neq | != | $map['id'] = array('neq',100); | id != 100 |
| gt | > | $map['id'] = array('gt',100); | id > 100 |
| egt | >= | $map['id'] = array('egt',100); | id >= 100 |
| lt | < | $map['id'] = array('lt',100); | id < 100 |
| elt | <= | $map['id'] = array('elt',100); | id <= 100 |
| like | like | $map<'username'> = array('like','Admin%'); | username like 'Admin%' |
| between | between and | $map['id'] = array('between','1,8'); | id BETWEEN 1 AND 8 |
| not between | not between and | $map['id'] = array('not between','1,8'); | id NOT BETWEEN 1 AND 8 |
| in | in | $map['id'] = array('in','1,5,8'); | id in(1,5,8) |
| not in | not in | $map['id'] = array('not in','1,5,8'); | id not in(1,5,8) |
| and(默认) | and | $map['id'] = array(array('gt',1),array('lt',10)); | (id > 1) AND (id < 10) |
| or | or | $map['id'] = array(array('gt',3),array('lt',10), 'or'); | (id > 3) OR (id < 10) |
| xor(异或) | xor | 两个输入中只有一个是true时,结果为true,否则为false,例子略。 | 1 xor 1 = 0 |
| exp | 综合表达式 | $map['id'] = array('exp','in(1,3,8)'); | $map['id'] = array('in','1,3,8'); |
同 SQL 一样,ThinkPHP运算符不区分大小写,eq 与 EQ 一样。
between、 in 条件支持字符串或者数组,即下面两种写法是等效的:
$map['id'] = array('not in','1,5,8');
$map['id'] = array('not in',array('1','5','8'));上表中的 exp 不是一个运算符,而是一个综合表达式以支持更复杂的条件设置。exp 的操作条件不会被当成字符串,可以使用任何 SQL 支持的语法,包括使用函数和字段名称。
exp 不仅用于 where 条件,也可以用于数据更新,如:
$Dao = M("Article");
// 构建 save 的数据数组,文章点击数+1
$data['id'] = 10;
$data['counter'] = array('exp','counter+1');
// 根据条件保存修改的数据
$User->save($data);
| 版本 | 新增功能 |
|---|---|
| 5.0.9 | 比较运算增加闭包子查询支持 |
| 5.0.4 | 支持对同一个字段多次调用查询方法 |
查询表达式支持大部分的SQL查询语法,也是ThinkPHP查询语言的精髓,查询表达式的使用格式:
where('字段名','表达式','查询条件');whereOr('字段名','表达式','查询条件');表达式不分大小写,支持的查询表达式有下面几种,分别表示的含义是:
| 表达式 | 含义 |
|---|---|
| EQ、= | 等于(=) |
| NEQ、<> | 不等于(<>) |
| GT、> | 大于(>) |
| EGT、>= | 大于等于(>=) |
| LT、< | 小于(<) |
| ELT、<= | 小于等于(<=) |
| LIKE | 模糊查询 |
| [NOT] BETWEEN | (不在)区间查询 |
| [NOT] IN | (不在)IN 查询 |
| [NOT] NULL | 查询字段是否(不)是NULL |
| [NOT] EXISTS | EXISTS查询 |
| EXP | 表达式查询,支持SQL语法 |
| > time | 时间比较 |
| < time | 时间比较 |
| between time | 时间比较 |
| notbetween time | 时间比较 |
表达式查询的用法示例如下:
例如:
where('id','eq',100);where('id','=',100);和下面的查询等效
where('id',100);表示的查询条件就是 id = 100
例如:
where('id','neq',100);where('id','<>',100);表示的查询条件就是 id <> 100
例如:
where('id','gt',100);where('id','>',100);表示的查询条件就是 id > 100
例如:
where('id','egt',100);where('id','>=',100);表示的查询条件就是 id >= 100
例如:
where('id','lt',100);where('id','<',100);表示的查询条件就是 id < 100
例如:
where('id','elt',100);where('id','<=',100);表示的查询条件就是 id <= 100
例如:
where('name','like','thinkphp%');查询条件就变成 name like 'thinkphp%'
V5.0.5+版本开始,like查询支持使用数组
where('name','like',['%think','php%'],'OR');查询条件支持字符串或者数组,例如:
where('id','between','1,8');和下面的等效:
where('id','between',[1,8]);查询条件就变成 id BETWEEN 1 AND 8
查询条件支持字符串或者数组,例如:
where('id','not in','1,5,8');和下面的等效:
where('id','not in',[1,5,8]);查询条件就变成 id NOT IN (1,5, 8)
[NOT] IN查询支持使用闭包方式
查询字段是否(不)是Null,例如:
where('name', null);where('title','null');where('name','not null');如果你需要查询一个字段的值为字符串null或者not null,应该使用:
where('title','=', 'null');where('name','=', 'not null');支持更复杂的查询情况 例如:
where('id','in','1,3,8');可以改成:
where('id','exp',' IN (1,3,8) ');exp查询的条件不会被当成字符串,所以后面的查询条件可以使用任何SQL支持的语法,包括使用函数和字段名称。
2023-08-27
我们在做网站得时候,有时候会为了防止人家拿代码 复制 保存等功能。来做一点防御方法,但是这种只能仿菜鸟,没法彻底防御,下面是代码,vararr=[123,17,18];//先定义下要禁止哪些键,document.oncontextmenu=newFunction("event.returnValue=false;"),//禁用
阅读更多2022-12-10
// flex空白元素填充 function flex_empty(listbox, ele, num, emptyclass) { var len = listbox.find(ele).length; var need = Math.ceil(len / num) * num - len; &nb
阅读更多2024-08-19
修改文件/apps/home/controller/ExtLabelController.php1,找到代码$this->test();在下方添加代码$this->ismobile();2,新增ismobile()函数//判断是否移动端设备privatefunctionismobile(){&nbs
阅读更多2023-03-15
解决办法,直接放二个判断即可,没有子栏目的判断{pboot:if('[nav:scode]'=='{sort:scode}')}selected{/pboot:if}点击了子栏目的判断{pboot:if('[nav:scode]'=='{sort:tcode}')}selected{/pboot:if}
阅读更多2021-12-14
最新7.0video.js响应式设置,配置和设置语言为繁体,先导入相应js和css <link href="/videojs/video-js.min.css" rel="stylesheet"> <script src="/videojs/video.min.js"></script> <script src="/videojs/lang/zh-TW.js"&g
阅读更多2023-07-10
pbootcms后台管理二次开发中如何使用{foreach}{if}{/if}{/foreach}语句,判断写入模板得数据,后台模板页面使用1.首先在相应的控制器里面获取数据assign到前端页面中例如$this->assign('NameData',$NameModel->getList());注意如果不是当前控制器对应的model,需要new一个新的model去查询数据。2.在htm
阅读更多2025-03-07
pb有时候给栏目加权限的时候,提示权限默认是2s页面跳转 有点麻烦,甚至不想要这个跳转提示页面按照路径找到 /core/function/handle.php把默认的1000改成你需要的时间 1000是1s 0就是直接跳转,但是0在直接跳转时候 会有一个页面马上闪烁跳转,可以把这个页面暂时display: none。找到路径 /core/template/error.html找到之后
阅读更多2023-07-08
pboot:list 调用tag链接:{pboot:listnum=15order=sorting}&
阅读更多