This article summarizes the common query language in thinkphp, which is used for reference. I believe we can bring some help to thinkphp development. Specifically as follows:
First, the general inquiry:
There are at least three forms where the query is brought into the where condition, etc.
1, String form:
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 is not equal to
GT Greater than
EGT is greater than or equal to
LT is less than
ELT is less than or equal to
Like equivalence and the like in SQL
[NOT] BETWEEN Query Range
[NOT] In Query Collection
EXP refers to the use of standard SQL statements to achieve more complex situations
Common form:
$data [' Field name ']=array (' is an expression ', ' query condition ');
Furthermore
$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 (Array (' GT ', '), array (' LT ', #), ' and ');
$list = $user->where ($data)->select ();
Dump ($list);
$data [' username ']=array ("like", ' p% '), array (' Like ', ' h% '), ' or ');
Third, the combination of inquiries:
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 ();
Dump ($list);
Four, compound inquiry:
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 ();
Dump ($list);
Equivalent
(id<30) and (Username=pengyanjie) or (password like p%))
V. Statistical enquiries:
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, locate the query:
Examples are as follows:
$user =new advmodel (' user '),//Instantiate Advanced Model Advmodel
//$user =m (' user ', ' Commonmodel '), or Advmodel to inherit from Commonmodel
$list = $user->order (' id desc ')->getn (2);//Returns the third
dump ($list)
in the result; $list = $user->order (' id desc ')->last ();//Returns the last
$list = $user->order (' id desc ')->first ();//Returns the first
Seven, SQL query:
1.excute () is mainly used for updating and writing:
$Model = new Model ()//Instantiate a model object does not correspond to any datasheet
$Model->execute ("Update think_user set name= ' thinkphp ' wher e Status=1 ");
2.query () is mainly used for query:
$user =m ();
$list = $user->query (' select * from Aoli_user ORDER by id desc ');
Dump ($list);
Eight, dynamic query
Examples are as follows:
$user =m (' user ');
$list = $user->getbyusername (' Pengyanjie ');
$list = $user->getbyusername (' Pengyanjie ');
Dump ($list);
$user =new advmodel (' user ');
$list = $user->top5 ();//First 5
dump ($list);
Interested friends can debug and run this example in the Thinkphp project, I believe there will be a new harvest.