thinkphp where () conditional query

String condition

Direct query and operation using string conditions, for example:

  1. $ User = M ("User"); // Instantiate the User object
  2. $User->where('type=1 AND status=1')->select(); 

Copy code

The last SQL statement generated is

  1. 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:

  1. $Model->where("id=%d and username='%s' and
  2. xx='%f'",array($id,$username,$xx))->select();

Copy code

Or use:

  1. $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:

  1. $ User = M ("User"); // Instantiate the User object
  2. $map['name'] = 'thinkphp';
  3. $map['status'] = 1;
  4.  // Pass the query condition into the query method
  5. $User->where($map)->select(); 

Copy code

The last SQL statement generated is

  1. 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:

  1. $ map ['field1'] = array ('expression', 'query condition 1');
  2. $ map ['field 2'] = array ('expression', 'query condition 2');
  3. $ 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:

  1. $map['id']  = array('eq',100);

Copy code

Equivalent to the following query

  1. $map['id']  = 100;

Copy code

The query condition expressed is id = 100

NEQ : not equal to (<>)
For example:

  1. $map['id']  = array('neq',100);

Copy code

The query condition indicated is id <> 100

GT : greater than (>)
For example:

  1. $map['id']  = array('gt',100);

Copy code

The query condition indicated is id> 100

EGT : greater than or equal to (> =)
For example:

  1. $map['id']  = array('egt',100);

Copy code

The query condition indicated is id> = 100

LT : less than (<)
For example:

  1. $map['id']  = array('lt',100);

Copy code

The query condition indicated is id <100

ELT : less than or equal to (<=)
For example:

  1. $map['id']  = array('elt',100);

Copy code

The query condition indicated is id <= 100

[NOT] LIKE : LIKE with sql
For example:

  1. $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:

  1. 'DB_LIKE_FIELDS'=>'title|content'

Copy code

Words, use

  1. $map['title'] = 'thinkphp';

Copy code

The query condition will become name like '% thinkphp%',
support array method, for example

  1. $map['a'] =array('like',array('%thinkphp%','%tp'),'OR');
  2. $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:

  1. $map['id']  = array('between','1,8');

Copy code

Equivalent to the following:

  1. $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:

  1. $map['id']  = array('not in','1,5,8');

Copy code

Equivalent to the following:

  1. $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:

  1. $map['id']  = array('in','1,3,8');

Copy code

Can be changed to:

  1. $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:

  1. $ User = M ("User"); // Instantiate the User object
  2.  // Assignment of data object attributes to be modified
  3. $data['name'] = 'ThinkPHP';
  4. $ data ['score'] = array ('exp', 'score + 1'); // User's points plus 1
  5. $ 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

  1. $ User = M ("User"); // Instantiate the User object
  2. $map['name|title'] = 'thinkphp';
  3.  // Pass the query condition into the query method
  4. $User->where($map)->select(); 

Copy code

The query condition becomes name = 'thinkphp' OR title = 'thinkphp'

Second, realize different query conditions for different fields

  1. $ User = M ("User"); // Instantiate the User object
  2. $map['status&title'] =array('1','thinkphp','_multi'=>true);
  3.  // Pass the query condition into the query method
  4. $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:

  1. $map['id'] = array(array('gt',1),array('lt',10)) ;

Copy code

The obtained query condition is: (`id`> 1) AND (` id` <10)

  1. $map['id'] = array(array('gt',3),array('lt',10), 'or') ;

Copy code

The obtained query condition is: (`id`> 3) OR (` id` <10)

  1. $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:

  1. $map['name']  = array(array('like','%a%'), array('like','%b%'), array('like','%c%'), 'ThinkPHP','or'); 

Copy code

The final query condition is:

  1. (`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:

  1. $ User = M ("User"); // Instantiate the User object
  2. $map['id'] = array('neq',1);
  3. $map['name'] = 'ok';
  4. $map['_string'] = 'status=1 AND score>10';
  5. $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.

  1. $map['id'] = array('gt','100');
  2. $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:

  1. $where['name']  = array('like', '%thinkphp%');
  2. $where['title']  = array('like','%thinkphp%');
  3. $where['_logic'] = 'or';
  4. $map['_complex'] = $where;
  5. $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:

  1. $where['id'] = array('gt',1);
  2. $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:

  1. $map['a'] = array('gt',1);
  2. $where['b'] = 1;
  3. $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.

Published 23 original articles · praised 2 · visits 5248

Guess you like

Origin blog.csdn.net/bianlitongcn/article/details/99826422