TP5 Database DB query operation

Source: Internet
Author: User

$data = Db::query('select * from tf_action');
$data = Db::query('select * from tf_action where id > ? and id < ?',[1,10]);
$sql = Db::getLastSql();

Queries with query.

Delete, add, modify, use Execute.

$data = Db::table('tf_action')->select();

The full name of the table is used here.

$data = Db::name('action')->select();

Here is the name of the table with the prefix removed.

$data = db('action')->select();

Helper function, the effect is similar to Db::name.

But it's not exactly the same.

$data = db('action')->where('id','>',1)->where('id','<',9)->select();        

Multi-criteria queries.

$data = db('action')->where('id','>',20)->whereOr('id','<',9)->select();        

or query.

If the middle condition is empty, it means =.

$where = new Where();$where['name'] = ['like','%户%'];$where['id'] = ['>',1];$data = db('action')->where($where)->select();
$where[] = ['name','like','%户%'];$where[] = ['id','>',1];$data = db('action')->where($where)->select();

Combine queries.

$where = new Where();$where['name'] = ['like','%户%'];$where['id'] = ['>',1];$data = db('action')->where($where)->limit(2,2)->order('id desc')->select();

Pagination sorting.

$where = new Where();$where['name'] = ['like','%户%'];$where['id'] = ['>',1];$data = db('action')->where($where)->limit(2,2)->order('id desc')->field('id,name')->select();

queries the specified field.

$where = new Where();$where['name'] = ['like','%户%'];$where['id'] = ['>',1];$data = db('action')->where($where)->limit(2,2)->order('id desc')->field('id aid,name')->select();

Alias.

$data = db('action')->where($where)->field('count(*) as count')->find();

Use System functions.

$data = db('action')->where("name like '%户%' AND id > 1")->select();

It is ok to write the string directly.

TP5 Database DB query operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.