#### ---上---
批量查询
~~~
$result = Db::name('user')
->where('name','like','%m%')
->where('id',['in',[1,2,3]],['between','5,8'],'or')
->limit(10)->select();
~~~
dump($result);
用 table(),要加前缀。
快捷查询
~~~
where('id&age','>',0);
where('id|age','>',0);
~~~
视图查询
~~~
$result =
Db::view('user','id,name,age')
->view('score_list','id,score','user.id = score_list.id')
->select();
dump($result);
~~~
#### ---中---
闭包查询
~~~
$result = Db::name('data')
->select(function($query){$query->where('name','like','%think%')
->where('id','in','1,2,3')->limit(10);
});
$result = Db::name('data')
->select(function($query){$query->where('name','like','%think%')
->where('id',' between',[1,3])->limit(10);
});
dump($result);
select * from think_data where name like '%think%' and id in ('1','2','3') limit 10;
~~~
Query 对象查询
~~~
$query = new \think\db\Query;
$query ->name('city')
->where('name','like','%think%')
->where('id','in','1,2,3')
->limit(10);
$result = Db::select($query);
dump($result);
~~~
获取数值
~~~
$name= Db::name('data')
->where('id',8)
->value('name');
dump($name);
$result = Db::table('user')
->where('id','exp','>1')
->value('name');
dump($result);
有两条,取得第一条
~~~
获取列数据
并且以id为索引
~~~
$list = Db::name('data')
->where('status',1)
->column('name','id');
~~~
####---下---
聚合查询
~~~
Db::name('data')
->where('id','>',1)
->count();
Db::name('data')
->where('id','>',1)
->max('age');
~~~
字符串查询
~~~
$result = Db::table('user')
->where('id>:id and name is not null',['id'=>10])
->select();
dump($result);
~~~
日期时间查询
查询大于某日的数据
~~~
$result = Db::table('user')
->whereTime('create_time','>','2017-01-01')
->select();
dump($result);
~~~
查询本周的数据
~~~
$result = Db::table('user')
->whereTime('create_time','week')
->select();
dump($result);
~~~
查询最近两天添加的数据
~~~
$result = Db::table('user')
->whereTime('create_time','-2 days')
->select();
dump($result);
~~~
查询一个时间范围的数据
~~~
$result = Db::table('user')
->whereTime('create_time','between',['2017-1-1','2017-1-10'])
->select();
dump($result);
~~~
查询上周的数据
~~~
$result = Db::table('user')
->whereTime('create_time','last week')
->select();
dump($result);
~~~