Summary of Common Query Languages in ThinkPHP

  • 2021-07-16 01:54:11
  • OfStack

This paper summarizes the common query languages in ThinkPHP for your reference. I believe it can bring 1 certain help to everyone's ThinkPHP development. The details are as follows:

1. General inquiry:

There are at least three forms of where conditions in queries

1. String form:


'id>5 and id<9'

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

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 is greater than
EGT is greater than or equal to
LT is less than
ELT is less than or equal to
LIKE is equivalent to like in sql
[NOT] BETWEEN Query Interval
[NOT] IN Query Set
EXP refers to the use of standard SQL statements to achieve more complex situations

Common forms:


$data[' Field name ']=array(' Is an expression ',' Query criteria ');

In addition


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

Is actually equivalent to


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

Examples are as follows:


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

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

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

3. Combine queries:

Examples are 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:

Examples are as follows:


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

0

Equivalent to


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

1

5. Statistical query:

Examples are as follows:


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

2

6. Location query:

Examples are as follows:


$user=new AdvModel('user');// Instantiate a high-level model AdvModel
//$user=M('user','CommonModel');// Or will AdvModel Use CommonModel To inherit 
$list=$user->order('id desc')->getN(2);// Returns the first in the result 3 Article 
dump($list);

$list=$user->order('id desc')->last();// Return to the last 1 Article 
$list=$user->order('id desc')->first();// Return to the 1 Article 

7. SQL query:

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


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

4

2. query () is primarily used for queries:


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

5

8. Dynamic query

Examples are as follows:


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

6

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

7

Interested friends can debug and run this example in ThinkPHP project, and I believe there will be new gains.


Related articles: