This article mainly introduces thinkphp range query, statistical query, and SQL direct query. It analyzes the common Query Techniques of ThinkPHP in details in the form of examples, which is very useful, for more information, see
This article mainly introduces thinkphp range query, statistical query, and SQL direct query. It analyzes the common Query Techniques of ThinkPHP in details in the form of examples, which is very useful, for more information, see
This example describes the thinkphp interval query, statistical query, and SQL direct query. Share it with you for your reference. The specific method is as follows:
I. interval query:
The Code is as follows:
$ Data ['id'] = array ('gt ', 4), array ('lt', 10); // The default relationship is (and) and
// SELECT * FROM 'tp _ user' WHERE ('id'> 4) AND ('id' <10 ))
$ Data ['id'] = array ('gt ', 4), array ('lt', 10), 'or') // the link is (or) or relationship
$ Data ['name'] = array ('like', '% 100'), array ('like',' % '), 'gege ', 'or ');
Multiple Arrays can be added to the array. If there is no or, the default is the and relationship.
Ii. statistical query:
Count // obtain the number
Max // obtain the maximum number
Min // obtain the minimum number
Avg // obtain the average
Sum // get the sum
The Code is as follows:
$ M = M ('user ');
$ Arr = $ m-> count (); // obtain the total number of users.
$ Arr = $ m-> where ("username = 'gege'")-> count (); // place the string
$ M = M ('user ');
$ Data ['username'] = 'gege'; // place an array
$ C = $ m-> where ($ data)-> count (); // The array operation is more standard.
Iii. SQL direct query, which provides great flexibility for more operations.
A. The primary number of queries processes the data read. The result set is returned successfully. If the query fails, the return value is boolean false.
The Code is as follows:
$ M = M ();
$ Result = $ m-> query ("select * from tp_user where id> 50 ");
Var_dump ($ result );
B. execute is used to update write operations. If a write operation is successful, the number of affected rows is returned. If a write operation fails, the return value is boolean false.
The Code is as follows:
$ M = M ();
$ Result = $ m-> execute ("insert into tp_user ('username') values ('ztz3 ')");
Var_dump ($ result );
I hope this article will help you with ThinkPHP framework programming.