ThinkPHPwhere method detailed explanation classification: PHP Time: April 22, 2016
In the ThinkPHP framework, the usage of the where method is the essence of the query language and an important part of ThinkPHP's coherent operations. The where method supports strings, arrays, and objects. array query is very powerful and officially recommended. The where method can be used to perform queries including common queries, expression queries, quick queries, interval queries, and combined queries. The following PHP programmer Lei Xuesong will explain in detail the usage of the where method.
1. string query
M ("User")-> where ('status = 0')-> select (); // query records whose status is 0
2. array query
A. common query
$ Where ['status'] = 0; // The field is the subscript of the array, and the value is the corresponding condition M ("User ") -> where (array ('status' => '0')-> select (); // query records whose status is 0
Note: the subscript of the array must be a database field, otherwise it will be filtered.
B. expression query
| Expression |
Description |
| EQ |
Equal to (=) |
| NEQ |
Not equal to (<>) |
| GT |
Greater than (>) |
| EGT |
Greater than or equal to (> =) |
| LT |
Less than (<) |
| ELT |
Less than or equal to (<=) |
| LIKE |
Fuzzy search |
| [NOT] |
(Not in) interval query |
| [NOT] IN |
(Not IN) IN query |
| EXP |
Expression query, supporting SQL syntax |
$ Where ['status'] = array ("eq", 0); // The field is the subscript of the array, and the value is the corresponding condition M ("User ") -> where ($ where)-> select (); // query records whose status is 03. example of where method array query
1. how to use arrays to implement multiple conditions for the same field? For example, you can query records whose createdate value is greater than or equal to April 20, 2016 and less than or equal to April 23, 2016.
$ Where ['createdate'] = array ('egt', "2016-04-20"), array ('elt', "2016-04-23 ")); // use a two-dimensional array to solve multiple conditions of the same field M ("User")-> where ($ where)-> select (); // query records whose createdate value is greater than or equal to April 20, 2016 and less than or equal to April 23, 2016
2. use OR for array query? For example, query records whose status is equal to 0 or 1.
$ Where ['status'] = array ('EQ ', "0"), array ('EQ', "1"), "OR "); // use a two-dimensional array to solve multiple conditions of the same field M ("User")-> where ($ where)-> select (); // query records whose status is equal to 0 or 1
3. how many conditions have an array query with both and or? For example, query records whose createdate is less than or equal to April 20, 2016 and whose status is 1 or level is 0.
$ Where ['createdate'] = array ('egt', "2016-04-20"); $ where ['status'] = '1 '; $ condition ['_ lock'] = "AND"; $ map [' _ compute'] = $ where; $ map ['level'] = '0 '; $ map ['_ logic'] = 'OR'; M ("User")-> where ($ map)-> select (); // query records whose createdate is less than or equal to April 20, 2016 and whose status is 1 or level is 0