The common query language and ThinkPHP language in thinkphp are summarized. The common query language in ThinkPHP is summarized. the common query language in thinkphp is summarized in this example for your reference. I believe that ThinkPHP can be developed with a summary of common query languages in ThinkPHP and 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.
Thinkphp queries all the information, queries the entire table, and retrieves a field
$ Res = $ student-> field ('Id, name')-> select ();/* if you want to query all the IDs and names, the condition will not be written, otherwise, you cannot query all the data that is best followed by a limit. in case of large websites and massive data volumes, your query tool will be stuck and your browser will be refreshed! */
Query language questions in thinkphp!
As the application goes deeper, many questions will be solved.
Beginners should not take a critical eye. each function has its own practical usage significance. in the early stage, they should choose the method they like. when they reach the level of a master, they will pick a thorn.
Examples in this article summarize common query languages in ThinkPHP for your reference. I believe it will bring ThinkPHP development to you...