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 about the thinkphp interval query, statistical query, and SQL direct query, see the examples in this article. 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 relationship is "and" by default.
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.
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.