Home >Backend Development >PHP Tutorial >Query languages commonly used in ThinkPHP
This article mainly introduces a summary of common query languages in ThinkPHP. It is a commonly used technique in ThinkPHP. It is very practical in project development. Friends in need can refer to it.
This article summarizes the examples of ThinkPHP Commonly used query languages in are provided for your reference. I believe it can bring some help to everyone's ThinkPHP development. The details are as follows:
1. Ordinary query:
When the query brings in where conditions, etc., there are at least three forms
1. Characters 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 can be equal to
NEQ can not be equal to
GT can be greater than
EG can can be greater than or equal to
LT can ELT Less than or equal to
LIKE Equivalent to like
[NOT] BETWEEN in SQL
Commonly used forms:
$data['字段名']=array('是表达式','查询条件');
In addition
$data['liwenkai']='liwenkai';
is actually equivalent to
$data['liwenkai']=array('eq','liwenkai');
The example is as follows:
$data['username']=array('like','peng%'); $list=$user->where($data)->select();2. Interval query:
Examples are as follows:
$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');3. Combined query :
The example is as follows:
$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);
##4. Compound query:
Example 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',30); $list=$user->where($data)->select(); dump($list);is equivalent to
(id<30)and ( (username=pengyanjie) or (password like p%) )
5. Statistical query:
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');
6. Positioning query:
Examples are as follows :$user=new AdvModel('user');//实例化高级模型AdvModel //$user=M('user','CommonModel');//或者将AdvModel用CommonModel来继承 $list=$user->order('id desc')->getN(2);//返回结果中的第三条 dump($list); $list=$user->order('id desc')->last();//返回最后一条 $list=$user->order('id desc')->first();//返回第一条
7. SQL query:
1.excute() is mainly used for updating and Write:
$Model = new Model() // 实例化一个 model 对象 没有对应任何数据表 $Model->execute( "update think_user set name='thinkPHP' where status=1" );
2.query() is mainly used to query:
$user=M(); $list=$user->query('select * from aoli_user order by id desc'); dump($list);##8. Dynamic query
The example is as follows:
$user=M('user');
$list=$user->getByusername('pengyanjie');
$list=$user->getByusername('pengyanjie');
dump($list);
$user=new AdvModel('user'); $list=$user->top5();//前5条 dump($list);
The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to execute native SQL statements in thinkPHP frameworkThinkPHP implements conversion of database query result data to corresponding type
The above is the detailed content of Query languages commonly used in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!