Example of ThinkPHP5 Framework Implementing Simple Batch Query Function

  • 2021-10-15 10:02:05
  • OfStack

In this paper, an example is given to describe the simple batch query function realized by ThinkPHP5 framework. Share it for your reference, as follows:

EXP of TP5, batch query, aggregate query, etc.


<!--more-->
// Use EXP Conditional expression, indicating that it is followed by native SQL Expression 
$result = Db::table('think_inno')->where('id','exp',"<10 and name='asd'")->select();
dump($result);
// Use and And or Make a mixed query 
$result = Db::table('think_inno')
->where('name','like','%think%')   //name Similar %thinkphp%
->where('id',['in',[1,2,3]],['>=',1],'or')  //id In 1~3 Between, or id>=1
->limit(2)
->select();
// Batch query 
$result = Db::table('think_inno')  // Batch query 
->where(['id' => [['in',[1,2,3]],['>=',1],'or'], 'name' => ['like','%think%']]) //(id In 1~3 In or id>=1 ) And name Similar think
->limit(10)
->select();
// Quick query 
$result = Db::table('think_inno')->where('id&num','>',1)->select();
id&num Denote and ; id&num Denote or ; 
// About Views 
 The result of the query is used as a 1 A virtual table; TP Direct use in Db::view To use the view 
$result = Db::view('think_inno','id,name')......;  //think-inno Is the table name, id,name Is the field to look up 
// Use query Object 
$query = new \think\db\Query;$query->table('think_inno')->where('name','like','think')->where('id','>=','3')->limit(10);
$result = Db::select($query);
print_r($result);
// Gets a value of a column and a row 
$name = Db::table('think_inno')->where('id',10)->value('name');
print_r($name); // Get id For 10 Adj. name The value of the field 
// Get a column column
$name = Db::table('think_inno')->where('status',1)->column('name');
// Take out status For 1 Correspondence name All values of the column 
// Get a 1 Row find
// Get id Dataset of key names 
$name = Db::table('think_inno')->where('num',0)->column('*','id');
print_r($name);
// Aggregate query count , max , min , avg , sum
$count = Db::table('think_inno')->where('num',0)->count();// Get num For 0 The corresponding amount of data of 
$count = Db::table('think_inno')->where('num',2)->max('id');// Get num For 2 The largest of id

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

Hope that this article is based on ThinkPHP framework of PHP programming help.


Related articles: