Application of where Method explained

Source: Internet
Author: User

The use of the Where method is the essence of thinkphp query Language, is also an important part of thinkphp ORM and highlights, can be completed including ordinary query, expression query, quick query, interval query, combined query, including query operations. The arguments of the Where method support strings and arrays, although objects can also be used but are not recommended.

String condition

$User = M ("User"); Instantiate the User object $user->where (' type=1 and Status=1 ')->select ();

SELECT * from Think_user WHERE type=1 and Status=1

Array condition

General Query

$User = M ("User"); Instantiate the User object $map[' name '] = ' thinkphp '; $map [' status '] = 1; Pass query condition into Query method $user->where ($map)->select ();

SELECT * from Think_user WHERE ' name ' = ' thinkphp ' and Status=1

An expression query

$map [' Field 1 ']  = array (' expression ', ' query Condition 1 '), $map [' Field 2 ']  = array (' expression ', ' query Condition 2 '), $Model->where ($map)->select ( ); also supports
$map [' id ']  = Array (' eq ', 100);

The query condition represented is ID = 100

$map [' id ']  = Array (' NEQ ', 100);

The query condition represented is ID <> 100

$map [' id ']  = array (' GT ', 100);

The query condition represented is ID > 100

$map [' id ']  = Array (' EGT ', 100);

The query condition represented is ID >= 100

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

The query condition represented is ID < 100

$map [' id ']  = Array (' ELT ', 100);

The query condition represented is ID <= 100

like: similar to SQL

$map [' name '] = Array (' Like ', ' thinkphp% ');

Query condition becomes name like ' thinkphp% '

$map [' A '] =array (' Like ', Array ('%thinkphp% ', '%tp '), ' OR '), $map [' B '] =array (' Notlike ', Array ('%thinkphp% ', '%tp '), ' and ');

The resulting query condition is: (A like '%thinkphp% ' OR a Like '%tp ') and (b is not like '%thinkphp% ' and B ' is not like '%TP ')

[NOT] between : With SQL [not] between, the query condition supports strings or arrays, for example:

$map [' id ']  = Array (' Between ', ' 1,8 ');
$map [' id ']  = Array (' Between ', Array (' 1 ', ' 8 '));

[NOT]in: With SQL [NOT], the query condition supports strings or arrays, for example:

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

EXP: expression to support more complex query conditions

$map [' id ']  = Array (' exp ', ' in (1,3,8) ');

Equivalent to

$map [' id ']  = Array (' In ', ' 1,3,8 ');

Combination Query

$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 ();

The resulting query condition becomes: (' id '! = 1) and (' name ' = ' OK ') and (Status=1 and SCORE>10)

Compound query

$where [' name ']  = Array (' Like ', '%thinkphp% '); $where [' title ']  = Array (' Like ', '%thinkphp% '); $where [' _logic ' ] = ' or '; $map [' _complex '] = $where; $map [' id ']  = array (' GT ', 1);

Equivalent to

$where [' id '] = array (' GT ', 1); $where [' _string '] = ' (Name like "%thinkphp%")  OR (title like "%thinkphp") ';

The query condition is
(ID > 1) and ((name like '%thinkphp% ') OR (title like '%thinkphp% '))

This article explains the application of the Where method for more information, please follow the PHP Chinese web.

Related recommendations:

Thinkphp the contents of the double loop traversal output

Introduction to ThinkPHP5 Quick Start method

Introduction to thinkphp Use steps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.