Query languages commonly used in ThinkPHP

不言
Release: 2023-03-30 16:34:01
Original
1268 people have browsed it

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'
Copy after login

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

In addition

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

is actually equivalent to

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

The example is as follows:

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

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

$data['username']=array(array('like','p%'),array('like','h%'),'or');
Copy after login

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

##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);
Copy after login

is equivalent to

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

5. Statistical query:

Examples are as follows:

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');
Copy after login

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

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" );
Copy after login

2.query() is mainly used to query:

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

##8. Dynamic query

The 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

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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!