1. 程式人生 > >tp5中的一些where操作

tp5中的一些where操作

null class func tab tween word ctu max result

快捷查詢

where(‘id&age‘,‘>‘,0);
where(‘id|age‘,‘>‘,0);

閉包查詢

$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);
});

獲取列數據,並且以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();

日期時間查詢

查詢大於某日的數據

$result = Db::table(‘user‘)
->whereTime(‘create_time‘,‘>‘,‘2017-01-01‘)
->select();

查詢本周的數據

$result = Db::table(‘user‘)
->whereTime(‘create_time‘,‘week‘)
->select();

查詢最近兩天添加的數據

$result = Db::table(‘user‘)
->whereTime(‘create_time‘,‘-2 days‘)
->select();

查詢一個時間範圍的數據

$result = Db::table(‘user‘)
->whereTime(‘create_time‘,‘between‘,[‘2017-1-1‘,‘2017-1-10‘])
->select();

查詢上周的數據

$result = Db::table(‘user‘)
->whereTime(‘create_time‘,‘last week‘)
->select();

tp5中的一些where操作