where(array('id'=>array('eq',10)))
where('id',10)
where('id','eq',10)
namespace app\index\controller;
use \think\Controller;
use \think\Db;
class Index extends Controller{
public function index(){
$data = array('name'=>'xiao1','create_time'=>time());
$rel = db('data')->where(array('id'=>array('eq',10)))->update($data);//方式1
$rel = db('data')->where('id',10)->update($data);//方式2
$rel = db('data')->where('id','eq',10)->update($data);//方式3
dump($rel);
}
}
输出结果
int(1)
$rel1 = db('data')->where('id=15 and type=0')->select();//方式1
编译结果
SELECT * FROM `tp_data` WHERE ( id=15 and type=0 )
$rel1 = db('data')->where("id=:id and type=:type")->bind(['id'=>15,'type'=>0])->select();//方式1
$rel2 = db('data')->where("id=? and type=?")->bind([15,0])->select();//方式2
$rel3 = db('data')->where("id=:id and type=:type",array('id'=>$id,'type'=>$type))->select();//方式3
$rel4 = db('data')->where("id=? and type=?",array($id,$type))->select();//方式4
编译结果
SELECT * FROM `tp_data` WHERE ( id='15' and type='0' )
$map = array(
'id'=>15,
'type'=>0,
);
$rel1 = db('data')->where($map)->select();//方式1
编译结果
SELECT * FROM `tp_data` WHERE `id` = 15 AND `type` = 0
表达式不分大小写,支持的查询表达式有下面几种,分别表示的含义是
表达式 | 含义 |
---|---|
EQ | 等于(=) |
NEQ | 不等于(<>) |
GT | 大于(>) |
EGT | 大于等于(>=) |
LT | 小于(<) |
ELT | 小于等于(<=) |
LIKE | 模糊查询 |
[NOT] BETWEEN | (不在)区间查询 |
[NOT] IN | (不在)IN 查询 |
示例1:查询id<10
和type=0
的数据
$map = array(
'id'=>array('lt',10),
'type'=>array('eq',0),
);
$rel1 = db('data')->where($map)->select();//方式1
编译结果
SELECT * FROM `tp_data` WHERE `id` < 10 AND `type` = 0
示例2:查询name like %xiao%
$map = array(
'name'=>array('like','%xiao%'),
);
$rel1 = db('data')->where($map)->select();//方式1
编译结果
SELECT * FROM `tp_data` WHERE `name` LIKE '%xiao%'
示例3:查询name like %xiao% or name like %rufeike%
$map = array(
'name'=>array('like',array('%xiao%','%rufeike%'),'OR'),
);
$rel1 = db('data')->where($map)->select();//方式1
编译结果
SELECT * FROM `tp_data` WHERE (`name` LIKE '%xiao%' OR `name` LIKE '%rufeike%')
$map = array(
'id'=>array('between',array(1,2))//数组
);
$rel1 = db('data')->where($map)->select();//方式1
$map = array(
'id'=>array('between','1,2')//字符串
);
$rel2 = db('data')->where($map)->select();//方式2
编译结果
SELECT * FROM `tp_data` WHERE `id` BETWEEN 1 AND 2
$map = array(
'id'=>array('in','1,2')
);
$rel1 = db('data')->where($map)->select();//方式1
$map = array(
'id'=>array('in',array(1,2))
);
$rel2 = db('data')->where($map)->select();//方式2
编译结果
SELECT * FROM `tp_data` WHERE `id` IN (1,2)
//查询id=1 且 type = 1的数据
$map = array(
'id&type'=>1
);
$rel1 = db('data')->where($map)->select();//方式1
//查询id=1 或 type = 1的数据
$map = array(
'id|type'=>1
);
$rel1 = db('data')->where($map)->select();//方式1
编译结果
SELECT * FROM `tp_data` WHERE ( `id` = 1 AND `type` = 1 )
SELECT * FROM `tp_data` WHERE ( `id` = 1 OR `type` = 1 )
示例1 id > 1 AND id < 10
$map = array(
'id'=>array(array('gt',1),array('lt',10))
);
$rel1 = db('data')->where($map)->select();//方式1
编译结果
SELECT * FROM `tp_data` WHERE ( `id` > 1 AND `id` < 10 )
示例2 id < 10 OR id > 12
$map = array(
'id'=>array(array('lt',10),array('gt',12),'OR')
);
$rel1 = db('data')->where($map)->select();//方式1
编译结果
SELECT * FROM `tp_data` WHERE ( `id` < 10 OR `id` > 12 )
$map = array(
'id'=>array('gt',5)
);
$map2 = array(
'id'=>array('lt',15),
'type'=>array('eq',0),
);
$rel1 = db('data')->where($map)->where($map2)->select();//方式1
编译结果
SELECT * FROM `tp_data` WHERE ( `id` > 5 AND `id` < 15 ) AND `type` = 0