Example Analysis of thinkphp Interval Query Statistical Query and SQL Direct Query

  • 2021-08-05 09:18:29
  • OfStack

This paper describes the thinkphp interval query, statistical query and SQL direct query. Share it for your reference. The specific methods are as follows:

1. Interval query:

$data['id']=array(array('gt',4),array('lt',10));// The default relationship is (and) And the relationship   
//SELECT * FROM `tp_user` WHERE ( (`id` > 4) AND (`id` < 10) ) 
 
$data['id']=array(array('gt',4),array('lt',10),'or') // The relationship is (or) Or the relationship  
 
$data['name']=array(array('like','%2%'),array('like','%  %'),'gege','or');

Multiple arrays can be added to the array. If there is no or, the default is and relationship

2. Statistical query:

count//Number of fetches
max//Get the maximum number
min//Get Minimum
avg//Get Average
sum//Get the sum

$m=M('User');  
$arr=$m->count();// Get the total number of users  
$arr=$m->where("username='gege'")->count();  // Place a string  
 
$m=M('User'); 
$data['username']='gege';// Place an array  
$c=$m->where($data)->count();  // Array operations will be more standard

3. SQL direct query, very flexible can carry out more operations.

a and query mainly deal with the result set of read data and return data successfully, and return boolean false if it fails

$m=M();  
$result=$m->query("select *  from tp_user where id >50"); 
var_dump($result);

b and execute are used to update each write operation, and the number of affected rows is returned successfully, and boolean false is returned if it fails
$m=M();  
$result=$m->execute("insert into tp_user(`username`) values('ztz3')"); 
var_dump($result);

I hope this article is helpful to everyone's ThinkPHP framework programming.


Related articles: