Summary of common query languages in ThinkPHP

Source: Internet
Author: User
This article mainly introduces ThinkPHP's common query language summary, which is a common technique in ThinkPHP and is of great practical value in project development. if you need ThinkPHP, refer to ThinkPHP.

This example summarizes common query languages in ThinkPHP for your reference. I believe it can help ThinkPHP development. The details are as follows:

1. common query:

There are at least three forms of where conditions for a query.

1. string format:

'id>5 and id<9'

2. array format:

The sample code is as follows:

$user=M('user');$data['username']='liwenkai';$list=$user->where(array('username'=>'liwenkai'))->select();$list=$user->where($data)->select();

3. object format:

The sample code is as follows:

$user=M('user');$a=new stdClass();$a->username='liwenkai';$list=$user->where($a)->select();  

4. query expression:

Equal to EQ
NEQ is not equal
GT>
EGT greater than or equal
LT is less
ELT less than or equal
LIKE is equivalent to like in SQL
[NOT] BETWEEN Query Interval
[NOT] IN query set
EXP refers to the use of standard SQL statements for more complex scenarios.

Common forms:

$ Data ['Field name'] = array ('expression ', 'query condition ');

In addition

$data['liwenkai']='liwenkai';

It is equivalent

$data['liwenkai']=array('eq','liwenkai');

Example:

$data['username']=array('like','peng%');$list=$user->where($data)->select();

II. interval query:

Example:

$user=M('user');$data['id']=array(array('gt',20),array('lt',23),'and');$list=$user->where($data)->select();dump($list);

$data['username']=array(array('like','p%'),array('like','h%'),'or');

III. combined query:

Example:

$user=M('user');$data['username']='pengyanjie';$data['password']=array('eq','pengyanjie');$data['id']=array('lt',30);$data['_logic']='or';$list=$user->where($data)->select();dump($list);

IV. Composite query:

Example:

$user=M('user');$data['username']=array('eq','pengyanjie');$data['password']=array('like','p%');$data['_logic']='or';$where['_complex']=$where;$where['id']=array('lt',30);$list=$user->where($data)->select();dump($list);

Equivalent

(id<30)and ( (username=pengyanjie) or (password like p%) )

5. statistical query:

Example:

echo $user->count();echo '
';echo $user->max('id');echo '
';echo $user->where('id<30')->min('id');echo '
';echo $user->avg('id');echo '
';echo $user->sum('id');

6. locate and query:

Example:

$ User = new AdvModel ('user'); // instantiate the advanced model AdvModel // $ user = M ('user', 'commonmodel '); // or use CommonModel to inherit $ list = $ user-> order ('Id desc')-> getN (2 ); // The third dump ($ list) in the returned result; $ list = $ user-> order ('Id desc')-> last (); // return the last $ list = $ user-> order ('Id desc')-> first (); // return the first entry

VII. SQL query:

1. excute () is mainly used for update and write:

$ Model = new Model () // instantiate a model object without corresponding data tables $ Model-> execute ("update think_user set name = 'thinkphp' where status = 1 ");

2. query () is mainly used for query:

$user=M();$list=$user->query('select * from aoli_user order by id desc');dump($list);         

8. dynamic query

Example:

$user=M('user');$list=$user->getByusername('pengyanjie');$list=$user->getByusername('pengyanjie');dump($list);

$ User = new AdvModel ('user'); $ list = $ user-> top5 (); // The first five dump ($ list );

If you are interested, you can call and test the example in this article in The ThinkPHP project. I believe there will be new gains.

Related Article

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.