Home  >  Article  >  Backend Development  >  Query languages ​​commonly used in ThinkPHP

Query languages ​​commonly used in ThinkPHP

不言
不言Original
2018-06-08 10:01:031343browse

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:

&#39;id>5 and id<9&#39;

2. Array form:

The sample code is as follows:

$user=M(&#39;user&#39;);
$data[&#39;username&#39;]=&#39;liwenkai&#39;;
$list=$user->where(array(&#39;username&#39;=>&#39;liwenkai&#39;))->select();
$list=$user->where($data)->select();

3. Object form:

The sample code is as follows:

$user=M(&#39;user&#39;);
$a=new stdClass();
$a->username=&#39;liwenkai&#39;;
$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[&#39;字段名&#39;]=array(&#39;是表达式&#39;,&#39;查询条件&#39;);

In addition

$data[&#39;liwenkai&#39;]=&#39;liwenkai&#39;;

is actually equivalent to

$data[&#39;liwenkai&#39;]=array(&#39;eq&#39;,&#39;liwenkai&#39;);

The example is as follows:

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

2. Interval query:

Examples are as follows:

$user=M(&#39;user&#39;);
$data[&#39;id&#39;]=array(array(&#39;gt&#39;,20),array(&#39;lt&#39;,23),&#39;and&#39;);
$list=$user->where($data)->select();
dump($list);

$data[&#39;username&#39;]=array(array(&#39;like&#39;,&#39;p%&#39;),array(&#39;like&#39;,&#39;h%&#39;),&#39;or&#39;);

3. Combined query :

The example is as follows:

$user=M(&#39;user&#39;);
$data[&#39;username&#39;]=&#39;pengyanjie&#39;;
$data[&#39;password&#39;]=array(&#39;eq&#39;,&#39;pengyanjie&#39;);
$data[&#39;id&#39;]=array(&#39;lt&#39;,30);
$data[&#39;_logic&#39;]=&#39;or&#39;;
$list=$user->where($data)->select();
dump($list);

##4. Compound query:

Example As follows:

$user=M(&#39;user&#39;);
$data[&#39;username&#39;]=array(&#39;eq&#39;,&#39;pengyanjie&#39;);
$data[&#39;password&#39;]=array(&#39;like&#39;,&#39;p%&#39;);
$data[&#39;_logic&#39;]=&#39;or&#39;;
$where[&#39;_complex&#39;]=$where;
$where[&#39;id&#39;]=array(&#39;lt&#39;,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 &#39;<br>&#39;;
echo $user->max(&#39;id&#39;);
echo &#39;<br>&#39;;
echo $user->where(&#39;id<30&#39;)->min(&#39;id&#39;);
echo &#39;<br>&#39;;
echo $user->avg(&#39;id&#39;);
echo &#39;<br>&#39;;
echo $user->sum(&#39;id&#39;);

6. Positioning query:

Examples are as follows :

$user=new AdvModel(&#39;user&#39;);//实例化高级模型AdvModel
//$user=M(&#39;user&#39;,&#39;CommonModel&#39;);//或者将AdvModel用CommonModel来继承
$list=$user->order(&#39;id desc&#39;)->getN(2);//返回结果中的第三条
dump($list);

$list=$user->order(&#39;id desc&#39;)->last();//返回最后一条
$list=$user->order(&#39;id desc&#39;)->first();//返回第一条

7. SQL query:

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

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

2.query() is mainly used to query:

$user=M();
$list=$user->query(&#39;select * from aoli_user order by id desc&#39;);
dump($list);

##8. Dynamic query

The example is as follows:

$user=M(&#39;user&#39;);
$list=$user->getByusername(&#39;pengyanjie&#39;);
$list=$user->getByusername(&#39;pengyanjie&#39;);
dump($list);

$user=new AdvModel(&#39;user&#39;);
$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 framework

ThinkPHP 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!

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