String condition
Direct query and operation using string conditions, for example:
- $ User = M ("User"); // Instantiate the User object
- $User->where('type=1 AND status=1')->select();
Copy code
The last SQL statement generated is
- SELECT * FROM think_user WHERE type=1 AND status=1
Copy code
If you use version 3.1 or higher, when using string conditions, it is recommended to cooperate with the preprocessing mechanism to ensure more security, for example:
- $Model->where("id=%d and username='%s' and
- xx='%f'",array($id,$username,$xx))->select();
Copy code
Or use:
- $Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();
Copy code
If the $ id variable comes from a user submission or URL address, if the input is of a non-numeric type, it will be formatted as a numeric format for query operations.
The string preprocessing format type supports specifying numbers, strings, etc. For details, please refer to the parameter description of the vsprintf method.
Array condition
Where usage of array condition is the usage recommended by ThinkPHP.
General query
The simplest way to query an array is as follows:
- $ User = M ("User"); // Instantiate the User object
- $map['name'] = 'thinkphp';
- $map['status'] = 1;
- // Pass the query condition into the query method
- $User->where($map)->select();
Copy code
The last SQL statement generated is
- SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1
Copy code
Expression query
The above query condition is just a simple equality judgment. You can use query expressions to support more SQL query syntax. The format of query expressions is:
- $ map ['field1'] = array ('expression', 'query condition 1');
- $ map ['field 2'] = array ('expression', 'query condition 2');
- $ Model-> where ($ map)-> select (); // also supported
Copy code
The expressions are case-insensitive, and the supported query expressions are as follows, which respectively mean:
expression | meaning |
---|---|
EQ | Equal to (=) |
NEQ | Not equal to (<>) |
GT | Greater than (>) |
EGT | Greater than or equal (> =) |
LT | Less than (<) |
ELT | Less than or equal to (<=) |
LIKE | Fuzzy query |
[NOT] BETWEEN | (Not in) interval query |
[NOT] IN | (Not in) IN query |
EXP | Expression query, support SQL syntax |
Examples are as follows:
EQ : equal to (=)
For example:
- $map['id'] = array('eq',100);
Copy code
Equivalent to the following query
- $map['id'] = 100;
Copy code
The query condition expressed is id = 100
NEQ : not equal to (<>)
For example:
- $map['id'] = array('neq',100);
Copy code
The query condition indicated is id <> 100
GT : greater than (>)
For example:
- $map['id'] = array('gt',100);
Copy code
The query condition indicated is id> 100
EGT : greater than or equal to (> =)
For example:
- $map['id'] = array('egt',100);
Copy code
The query condition indicated is id> = 100
LT : less than (<)
For example:
- $map['id'] = array('lt',100);
Copy code
The query condition indicated is id <100
ELT : less than or equal to (<=)
For example:
- $map['id'] = array('elt',100);
Copy code
The query condition indicated is id <= 100
[NOT] LIKE : LIKE with sql
For example:
- $map['name'] = array('like','thinkphp%');
Copy code
The query condition becomes name like 'thinkphp%'.
If the DB_LIKE_FIELDS parameter is configured, some fields will be automatically fuzzy. For example, set:
- 'DB_LIKE_FIELDS'=>'title|content'
Copy code
Words, use
- $map['title'] = 'thinkphp';
Copy code
The query condition will become name like '% thinkphp%',
support array method, for example
- $map['a'] =array('like',array('%thinkphp%','%tp'),'OR');
- $map['b'] =array('notlike',array('%thinkphp%','%tp'),'AND');
Copy code
The generated query condition is:
(a like '% thinkphp%' OR a like '% tp') AND (b not like '% thinkphp%' AND b not like '% tp')
[NOT] BETWEEN : same as sql [ not] between, query conditions support string or array, for example:
- $map['id'] = array('between','1,8');
Copy code
Equivalent to the following:
- $map['id'] = array('between',array('1','8'));
Copy code
The query condition becomes id BETWEEN 1 AND 8
[NOT] IN : Same as [not] in of sql, the query condition supports string or array, for example:
- $map['id'] = array('not in','1,5,8');
Copy code
Equivalent to the following:
- $map['id'] = array('not in',array('1','5','8'));
Copy code
The query condition becomes id NOT IN (1,5, 8)
EXP : expression, which supports more complex query situations.
For example:
- $map['id'] = array('in','1,3,8');
Copy code
Can be changed to:
- $map['id'] = array('exp',' IN (1,3,8) ');
Copy code
The conditions of the exp query will not be treated as strings, so the following query conditions can use any syntax supported by SQL, including the use of function and field names.
Query expressions can be used not only for query conditions, but also for data updates, for example:
- $ User = M ("User"); // Instantiate the User object
- // Assignment of data object attributes to be modified
- $data['name'] = 'ThinkPHP';
- $ data ['score'] = array ('exp', 'score + 1'); // User's points plus 1
- $ User-> where ('id = 5')-> save ($ data); // Save modified data according to conditions
Copy code
Quick query
where method supports quick query, which can further simplify the writing of query conditions, for example:
1. Achieve the same query conditions for different fields
- $ User = M ("User"); // Instantiate the User object
- $map['name|title'] = 'thinkphp';
- // Pass the query condition into the query method
- $User->where($map)->select();
Copy code
The query condition becomes name = 'thinkphp' OR title = 'thinkphp'
Second, realize different query conditions for different fields
- $ User = M ("User"); // Instantiate the User object
- $map['status&title'] =array('1','thinkphp','_multi'=>true);
- // Pass the query condition into the query method
- $User->where($map)->select();
Copy code
'_multi' => true must be added at the end of the array, indicating that the current multi-condition matching, so the query condition becomes status = 1 AND title = 'thinkphp', the query field supports more, for example:
$ map ['status & score & title '] = array (' 1 ', array (' gt ',' 0 '),' thinkphp ',' _ multi '=> true); the
query condition becomes status = 1 AND score> 0 AND title =' thinkphp '
Note: "|" and "&" in the quick query method cannot be used at the same time.
Interval query
where method supports interval query of a field, for example:
- $map['id'] = array(array('gt',1),array('lt',10)) ;
Copy code
The obtained query condition is: (`id`> 1) AND (` id` <10)
- $map['id'] = array(array('gt',3),array('lt',10), 'or') ;
Copy code
The obtained query condition is: (`id`> 3) OR (` id` <10)
- $map['id'] = array(array('neq',6),array('gt',3),'and');
Copy code
The obtained query condition is: (`id`! = 6) AND (` id`> 3) The
last one can be an AND, OR or XOR operator. If it is not written, the default is the AND operation.
The condition of interval query can support all expressions of ordinary query, that is to say expressions like LIKE, GT and EXP can also be supported. In addition, interval query can also support more conditions, as long as the conditions for a field can be written together, for example:
- $map['name'] = array(array('like','%a%'), array('like','%b%'), array('like','%c%'), 'ThinkPHP','or');
Copy code
The final query condition is:
- (`name` LIKE '%a%') OR (`name` LIKE '%b%') OR (`name` LIKE '%c%') OR (`name` = 'ThinkPHP')
Copy code
Combined query
Combined queries are used for complex query conditions. If you need to occasionally use strings while querying but do not want to lose the flexibility of the array method, you can consider using combined queries.
The main body of the combined query is still an array query, but some special query support is added, including string mode query (_string), compound query (_complex), request string query (_query), special query in mixed query Only one query can be defined. Due to the array indexing method, special queries with the same index will be overwritten.
1. String mode query (using _string as the query condition)
Array conditions can also be mixed with string conditions, for example:
- $ User = M ("User"); // Instantiate the User object
- $map['id'] = array('neq',1);
- $map['name'] = 'ok';
- $map['_string'] = 'status=1 AND score>10';
- $User->where($map)->select();
Copy code
The final query condition becomes:
(`id`! = 1) AND (` name` = 'ok') AND (status = 1 AND score> 10)
2. Request string query method
Request string query is one This method is similar to the URL parameter passing method, and can support simple conditional equality judgment.
- $map['id'] = array('gt','100');
- $map['_query'] = 'status=1&score=100&_logic=or';
Copy code
The obtained query condition is: `id`> 100 AND (` status` = '1' OR `score` = '100')
3. Compound query
Compound query is equivalent to encapsulating a new query condition, and then merged into the original Among the query conditions, more complicated query condition assembly can be completed.
E.g:
- $where['name'] = array('like', '%thinkphp%');
- $where['title'] = array('like','%thinkphp%');
- $where['_logic'] = 'or';
- $map['_complex'] = $where;
- $map['id'] = array('gt',1);
Copy code
The query condition is
(id> 1) AND ((name like '% thinkphp%') OR (title like '% thinkphp%')) The
compound query uses _complex as a sub-query condition to define, with the previous query method, you can Very flexible to formulate more complex query conditions.
Many query methods can be converted to each other, for example, the above query conditions can be changed to:
- $where['id'] = array('gt',1);
- $where['_string'] = ' (name like "%thinkphp%") OR ( title like "%thinkphp") ';
Copy code
The resulting SQL statement is consistent.
Multiple calls
Starting from version 3.1.3, the where method supports multiple calls, but the string condition can only appear once, for example:
- $map['a'] = array('gt',1);
- $where['b'] = 1;
- $Model->where($map)->where($where)->where('status=1')->select();
Copy code
Multiple array conditional expressions will eventually merge, but string conditions are only supported once.