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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:20:041090browse

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'

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 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 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('是表达式','查询条件');

Also

$data['liwenkai']='liwenkai';

is actually equivalent to

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

An example is as follows:

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

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);
$data['username']=array(array('like','p%'),array('like','h%'),'or');

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

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

is equivalent to

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

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

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

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

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

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

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...
Statement:
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