Common Query Language in thinkphp

Source: Internet
Author: User
This article mainly introduced thinkphp in the common Query Language summary, is the common skill in the thinkphp, has the practical value in the project development, the need friend can refer to the next

In this paper, we summarize the common query language in thinkphp for your reference. I believe that can give you thinkphp development to bring some help. Specific as follows:

First, ordinary query:

There are at least three forms in which the query is brought into the where condition, etc.

1. String form:

' Id>5 and Id<9 '

2. Array form:

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 form:

The sample code is as follows:

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

4. Query expression:

EQ equals
NEQ Not equal to
GT Greater than
EGT greater than or equal to
LT is less than
ELT less than or equal to
Like equivalence vs. in SQL
[NOT] Between query interval
[NOT] In Query Collection
EXP refers to using standard SQL statements to achieve more complex situations

Common forms:

$data [' Field name ']=array (' is an expression ', ' query condition ');

In addition

$data [' Liwenkai ']= ' Liwenkai ';

is actually equivalent to

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

Examples are as follows:

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

Second, the interval query:

Examples are as follows:

$user =m (' user '), $data [' ID ']=array (' GT '), Array (' LT ', ' Max '), ' and '), $list = $user->where ($data)->select ();d UMP ($list);

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

Third, the combination of query:

Examples are as follows:

$user =m (' user '), $data [' username ']= ' Pengyanjie '; $data [' Password ']=array (' eq ', ' Pengyanjie '); $data [' ID ']=array (' Lt ', (); $data [' _logic ']= ' or '; $list = $user->where ($data)->select ();d UMP ($list);

Four, compound query:

Examples are as follows:

$user =m (' user '); $data [' Username ']=array (' eq ', ' Pengyanjie '); $data [' Password ']=array (' like ', ' p% '); $data [' _logic ' ]= ' or '; $where [' _complex ']= $where; $where [' ID ']=array (' lt ', '); $list = $user->where ($data)->select ();d UMP ($ list);

Equivalent

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

Five, Statistical inquiry:

Examples are as follows:

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

Six, positioning query:

Examples are as follows:

$user =new advmodel (' user ');//Instantiate Advanced Model advmodel//$user =m (' user ', ' Commonmodel ');//or Advmodel with Commonmodel to inherit $list=$ User->order (' id desc ')->getn (2);//Return the result of the third dump ($list); $list = $user->order (' id desc ')->last ();// Returns the last $list= $user->order (' id desc ')->first ();//returns the first one

Seven, SQL query:

1.excute () is primarily used for updates and writes:

$Model = new Model ()//instantiation of a Model object  does not correspond to any data table $model->execute ("Update think_user set name= ' thinkphp ' where statu S=1 ");

2.query () is mainly used for querying:

$user =m (); $list = $user->query (' select * from Aoli_user ORDER by id DESC ');d UMP ($list);

Eight, dynamic query

Examples are as follows:

$user =m (' user '); $list = $user->getbyusername (' Pengyanjie '); $list = $user->getbyusername (' Pengyanjie ');d UMP ($ list);

$user =new advmodel (' user '), $list = $user->top5 ();//First 5 dumps ($list);

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.