Home > Backend Development > PHP Tutorial > Summary of common query languages ​​in ThinkPHP, thinkphp language summary_PHP tutorial

Summary of common query languages ​​in ThinkPHP, thinkphp language summary_PHP tutorial

WBOY
Release: 2016-07-13 10:20:04
Original
1059 people have browsed it

Summary of commonly used query languages ​​in ThinkPHP, summary of thinkphp languages

The examples in this article summarize the commonly used query languages ​​in ThinkPHP for your reference. I believe it can bring some help to everyone's ThinkPHP development. The details are as follows:

1. General query:

In the query, bring in where conditions, etc., there are at least three forms

1. String format:

'id>5 and id<9'

Copy after login

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();

Copy after login

3. Object form:

The sample code is as follows:

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

Copy after login

4. Query expression:

EQ is equal to
NEQ is not equal to
GT Greater than
EGT Greater than or equal to
LT Less than
ELT Less than or equal to
LIKE is equivalent to like
in sql [NOT] BETWEEN Query range
[NOT] IN Query collection
EXP refers to using standard SQL statements to achieve more complex situations

Common forms:

$data['字段名']=array('是表达式','查询条件');
Copy after login

Also

$data['liwenkai']='liwenkai';
Copy after login

is actually equivalent to

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

An example is as follows:

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

Copy after login

2. Interval query:

An example is as follows:

$user=M('user');
$data['id']=array(array('gt',20),array('lt',23),'and');
$list=$user->where($data)->select();
dump($list);
Copy after login
$data['username']=array(array('like','p%'),array('like','h%'),'or');

Copy after login

3. Combination query:

An 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);

Copy after login

4. Compound query:

An example is 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);
Copy after login

is equivalent to

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

5. Statistical query:

An example is 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');

Copy after login

6. Positioning query:

An example is 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();//返回第一条

Copy after login

7. SQL query:

1.excute() is mainly used for updating and writing:

$Model = new Model() // 实例化一个 model 对象  没有对应任何数据表
$Model->execute( "update think_user set name='thinkPHP' where status=1" );

Copy after login

2.query() is mainly used for query:

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

8. Dynamic query

An example is as follows:

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

Copy after login
$user=new AdvModel('user');
$list=$user->top5();//前5条
dump($list);

Copy after login

Interested friends can debug and run the examples in this article in the ThinkPHP project. I believe there will be new gains.

thinkphp queries all the information, checks the entire table, and takes out a certain field

$res=$student->field('id,name')->select();/*If you want to query all ids and names, then the conditions will not be written, otherwise you will not be able to query all It is best to add a limit at the end to limit the amount of data. If it is a large website with massive data, your query tool will freeze and the browser will refresh! */

Query language in thinkphp Question!

With the deepening of use, many previous questions will be constantly answered.
Beginners should not be critical. Each function has its actual meaning. In the early stage, just choose the method you like. Wait until you are an expert to find faults.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/868243.htmlTechArticleA summary of common query languages ​​​​in ThinkPHP, thinkphp language summary This article summarizes the common query languages ​​​​in ThinkPHP for everyone For reference purposes only. I believe it can bring you a sense of ThinkPHP development...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template