设置查询条件
新增和更新操作必须要设置查询条件才能执行。
1、查询条件的2个方法
where():AND条件
whereOr():OR条件
2、查询条件的3种格式
where('字段名',表达式,查询条件)
where(['字段名'=>['表达式,'查询条]......])
where(function($query){//链式查询语句;})
举例:
<?php
namespace app\ index\controller;
use think\Db;
class Index
{
public function index( )
{
dump (
Db: :table( 'staff')
->field(['name', 'salary'])
->where( [
id'=>['>', 1003] ,
'salary'=>['>' I3000 ]
])
->select()
);
}
}
复杂的查询条件推荐使用闭包函数,举例:
<?php
namespace app\ index\controller;
use think\Db;
class Index
{
public function index( )
$salary = 4000 ;
dump(
Db: :table( 'staff')
->field([ 'name', 'salary'])
->where (function ($query) use ($salary){
$query->where( 'id', '>' , 1003)
->where( 'salary','>',$salary);
})
->select( );
}
}
|