Table of Contents
String condition
Array condition
Normal query
Expression query
组合查询
Home Backend Development PHP Tutorial Application explanation of where method

Application explanation of where method

Jun 15, 2018 am 10:34 AM
where

The usage of

where method is the essence of ThinkPHP query language and an important part and highlight of ThinkPHP ORM. It can complete query operations including ordinary query, expression query, quick query, interval query and combined query. The parameters of the where method support strings and arrays. Although objects can also be used, it is not recommended.

String condition

$User = M("User"); // 实例化User对象$User->where('type=1 AND status=1')->select();
Copy after login

SELECT * FROM think_user WHERE type=1 AND status=1

Array condition

Normal query

$User = M("User"); // 实例化User对象$map['name'] = 'thinkphp';$map['status'] = 1; // 把查询条件传入查询方法$User->where($map)->select();
Copy after login

SELECT * FROM think_user WHERE `name`='thinkphp' AND status=1

Expression query

$map['字段1']  = array('表达式','查询条件1');$map['字段2']  = array('表达式','查询条件2');$Model->where($map)->select(); // 也支持
Copy after login
$map['id']  = array('eq',100);
Copy after login

represents the query condition represented by id = 100

$map['id']  = array('neq',100);
Copy after login

The query condition is id <> 100

$map['id']  = array('gt',100);
Copy after login

represents the query condition is id > 100

$map['id']  = array('egt',100);
Copy after login

represents the query condition is id >= 100

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

represents The query condition is id < 100

$map[&#39;id&#39;]  = array(&#39;elt&#39;,100);
Copy after login

The query condition represented is id <= 100

[NOT] LIKE: Same as sql's LIKE

$map[&#39;name&#39;] = array(&#39;like&#39;,&#39;thinkphp%&#39;);
Copy after login

The query condition becomes name like 'thinkphp%'

$map[&#39;a&#39;] =array(&#39;like&#39;,array(&#39;%thinkphp%&#39;,&#39;%tp&#39;),&#39;OR&#39;);$map[&#39;b&#39;] =array(&#39;notlike&#39;,array(&#39;%thinkphp%&#39;,&#39;%tp&#39;),&#39;AND&#39;);
Copy after login

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's [not] between, query conditions support strings or arrays, for example:

$map[&#39;id&#39;]  = array(&#39;between&#39;,&#39;1,8&#39;);
Copy after login
$map[&#39;id&#39;]  = array(&#39;between&#39;,array(&#39;1&#39;,&#39;8&#39;));
Copy after login

[NOT] IN: 同sql的[not] in ,查询条件支持字符串或者数组,例如:

$map[&#39;id&#39;]  = array(&#39;not in&#39;,&#39;1,5,8&#39;);
Copy after login
$map[&#39;id&#39;]  = array(&#39;not in&#39;,array(&#39;1&#39;,&#39;5&#39;,&#39;8&#39;));
Copy after login

EXP:表达式,支持更复杂的查询情况

$map[&#39;id&#39;]  = array(&#39;exp&#39;,&#39; IN (1,3,8) &#39;);
Copy after login

等同于

$map[&#39;id&#39;]  = array(&#39;in&#39;,&#39;1,3,8&#39;);
Copy after login

组合查询

$User = M("User"); // 实例化User对象$map[&#39;id&#39;] = array(&#39;neq&#39;,1);$map[&#39;name&#39;] = &#39;ok&#39;;$map[&#39;_string&#39;] = &#39;status=1 AND score>10';$User->where($map)->select();</p>
<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 19.5px; font-size: 13px; font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(254, 254, 242); text-align: left;">最后得到的查询条件就成了:( `id` != 1 ) AND ( `name` = 'ok' ) AND ( status=1 AND score>10 )</p>
<p style="margin: 10px auto; padding-top: 0px; padding-bottom: 0px; line-height: 19.5px; font-size: 13px; font-family: Verdana, Arial, Helvetica, sans-serif; background-color: rgb(254, 254, 242); text-align: left;"><span style="margin:0px;padding:0px;">复合查询</span></p>
<pre style="margin-top:0px;margin-bottom:0px;padding:0px;white-space:pre-wrap;font-family:'Courier New' !important;" class="brush:php;toolbar:false;">$where['name']  = array('like', '%thinkphp%');$where['title']  = array('like','%thinkphp%');$where['_logic'] = 'or';$map['_complex'] = $where;$map['id']  = array('gt',1);
Copy after login

等同于

$where['id'] = array('gt',1);$where['_string'] = ' (name like "%thinkphp%")  OR ( title like "%thinkphp") ';
Copy after login

查询条件是 
( id > 1) AND ( ( name like '%thinkphp%') OR ( title like '%thinkphp%') )

本文讲解了where方法的应用更多相关内容请关注php中文网。

相关推荐:

ThinkPHP 双重循环遍历输出 的相关内容

ThinkPHP5快速入门 方法的介绍

介绍ThinkPHP使用步骤

The above is the detailed content of Application explanation of where method. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

A Practical Guide to the Where Method in Laravel Collections A Practical Guide to the Where Method in Laravel Collections Mar 10, 2024 pm 04:36 PM

Practical Guide to Where Method in Laravel Collections During the development of the Laravel framework, collections are a very useful data structure that provide rich methods to manipulate data. Among them, the Where method is a commonly used filtering method that can filter elements in a collection based on specified conditions. This article will introduce the use of the Where method in Laravel collections and demonstrate its usage through specific code examples. 1. Basic usage of Where method

How to use the Where method in Laravel collections How to use the Where method in Laravel collections Mar 10, 2024 pm 10:21 PM

How to use the Where method in Laravel collection Laravel is a popular PHP framework that provides a wealth of functions and tools to facilitate developers to quickly build applications. Among them, Collection is a very practical and powerful data structure in Laravel. Developers can use collections to perform various operations on data, such as filtering, mapping, sorting, etc. In collections, the Where method is a commonly used method for filtering the collection based on specified conditions.

From beginner to proficient: Master the skills of using is and where selectors From beginner to proficient: Master the skills of using is and where selectors Sep 08, 2023 am 09:15 AM

From beginner to proficient: Master the skills of using is and where selectors Introduction: In the process of data processing and analysis, the selector is a very important tool. Through selectors, we can extract the required data from the data set according to specific conditions. This article will introduce the usage skills of is and where selectors to help readers quickly master the powerful functions of these two selectors. 1. Use of the is selector The is selector is a basic selector that allows us to select the data set based on given conditions.

The basic usage of mysql left join and the difference between on and where The basic usage of mysql left join and the difference between on and where Jun 02, 2023 pm 11:54 PM

Preface When we write SQL statements, we cannot avoid using connection keywords, such as inner connections and outer connections. There are many types. I will post here a picture I found elsewhere: I think this picture is very detailed. It shows the common link types in SQL statements. Take leftjoin in this article as an example. It is available online. As defined: the LEFTJOIN keyword will return all rows from the left table, even if there are no matching rows in the right table. In fact, literally speaking, leftjoin is relatively easy to understand, but there are still some problems during use. For example, if the condition is after on and after where, their results are completely different. Let's go from shallow to deep

Usage analysis of Where method in Laravel collection Usage analysis of Where method in Laravel collection Mar 09, 2024 pm 06:51 PM

Laravel is a popular PHP development framework that provides rich and convenient functions. Collection is one of the very important data structures in Laravel. The collection class provides many powerful methods, one of which is the where method. This article will use specific code examples to analyze the usage of the where method in Laravel collections. 1. Create a collection First, we need to create a collection that contains some data. Can

Common errors and solutions to where method in Laravel Common errors and solutions to where method in Laravel Mar 10, 2024 pm 06:03 PM

Common errors and solutions for the where method in Laravel. In the process of developing using the Laravel framework, we often use EloquentORM to operate the database. Among them, the where method is a very commonly used method for filtering data in the database. However, due to lack of familiarity with the Laravel framework or lack of deep understanding of EloquentORM, it is easy to make some common mistakes when using the where method. This article will introduce several common w

Detailed explanation of Where method of Laravel collection Detailed explanation of Where method of Laravel collection Mar 10, 2024 pm 01:33 PM

Laravel is a popular PHP framework, and its Collections class provides powerful data processing functions. Among them, the Where method is one of the commonly used methods in collection classes, used to filter data that meets conditions. This article will introduce the Where method of Laravel collection in detail, including usage methods, parameter meanings, and specific code examples. 1. Overview of the Where method The Where method is used to filter elements in the collection that meet specified conditions and return a

How to use the WHERE clause in SQL to specify selection criteria How to use the WHERE clause in SQL to specify selection criteria Jun 03, 2023 pm 04:31 PM

SQLWHERE clause The WHERE clause is used to specify the selection criteria. To conditionally select data from a table, add a WHERE clause to the SELECT statement. The syntax is as follows: SELECT column name FROM table name WHERE column operator value The following operators can be used in the WHERE clause: =: equal to: not equal to >: greater than =: greater than or equal to 1965

See all articles