Home > Backend Development > PHP Tutorial > Detailed explanation of where() usage in ThinkPHP

Detailed explanation of where() usage in ThinkPHP

不言
Release: 2023-03-30 07:56:01
Original
2527 people have browsed it

This article mainly introduces the detailed explanation of the usage of where() in ThinkPHP. It has certain reference value. Now I share it with you. Friends in need can refer to it.

The where method can be used to database Filter the results of the operation. That is, the where clause in the SQL query statement. This article introduces to you the detailed explanation of the usage of where() in ThinkPHP. Interested friends can refer to

This article introduces the usage of where() method in ThinkPHP. The where method can be used to filter the results of database operations. That is, the where clause in the SQL query statement.

Today I will tell you about the most commonly used but also the most complex where method for querying. The where method is also one of the coherent operation methods of the model class and is mainly used for querying and setting operating conditions.

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

String conditions

Use string conditions to directly query and operate, for example:

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

The last generated SQL statement is

SELECT * FROM think_user WHERE type=1 AND status=1
Copy after login

If you use version 3.1 or above, it is recommended to cooperate with preprocessing when using string conditions mechanism to ensure more security, for example:

$Model->where("id=%d and username='%s' and
xx='%f'",array($id,$username,$xx))->select();
Copy after login

or use:

$Model->where("id=%d and username='%s' and xx='%f'",$id,$username,$xx)->select();
Copy after login

if If the $id variable comes from user submission or URL address, if it is a non-numeric type, it will be forced to be formatted into numeric format before querying.
The string preprocessing format type supports specifying numbers, strings, etc. For details, please refer to the parameter description of the vsprintf method.

Array condition

The where usage of array condition is the usage recommended by ThinkPHP.

Normal query

The simplest array query method is as follows:

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

The final generated SQL statement is

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

expression query

above The query condition is just a simple equality judgment. You can use query expressions to support more SQL query syntax. The usage format of query expressions is:

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

Expressions are not case-sensitive. The following query expressions are supported, and their respective meanings are:

Expression meaning

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 query
[NOT] BETWEEN (not in) interval query
[NOT] IN (not in)IN query
EXP expression query, supports SQL syntax

Examples are as follows:

EQ: Equals (=)

For example:

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

and the following query The equivalent query condition represented by

$map[&#39;id&#39;] = 100;
Copy after login

is id = 100

NEQ: Not equal to (<>)

For example:

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

The query condition represented is id <> 100

GT: greater than (>)

For example:

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

The query condition represented is id > 100

EGT: greater than or equal to (>=)

For example:

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

represents the query condition id >= 100

LT: less than (<)

For example:

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

The query condition represented is id < 100

ELT: Less than or equal to (<=)

For example:

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

represents the query condition id <= 100

[NOT] LIKE: Same as sql LIKE

For example:

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

The query condition becomes name like 'thinkphp%'

If the DB_LIKE_FIELDS parameter is configured , some fields will also automatically perform fuzzy queries. For example, if you set:

&#39;DB_LIKE_FIELDS&#39;=>&#39;title|content&#39;
Copy after login

, the query conditions will change if you use

$map[&#39;title&#39;] = &#39;thinkphp&#39;;
Copy after login

The name like '%thinkphp%'
supports array mode. For example,

$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

generates query conditions:

(a like &#39;%thinkphp%&#39; OR a like &#39;%tp&#39;) AND (b not like &#39;%thinkphp%&#39; AND b not like &#39;%tp&#39;)
Copy after login

[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

is equivalent to the following:

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

The query condition becomes id BETWEEN 1 AND 8

[NOT] IN: Same as sql [not] in, query conditions support strings or arrays, for example:

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

is equivalent to the following:

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

The query condition becomes id NOT IN (1,5, 8)

EXP: Expression, supports more complex query situations

For example:

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

可以改成:

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

exp查询的条件不会被当成字符串,所以后面的查询条件可以使用任何SQL支持的语法,包括使用函数和字段名称。

查询表达式不仅可用于查询条件,也可以用于数据更新,例如:

$User = M("User"); // 实例化User对象
// 要修改的数据对象属性赋值
$data[&#39;name&#39;] = &#39;ThinkPHP&#39;;
$data[&#39;score&#39;] = array(&#39;exp&#39;,&#39;score+1&#39;);// 用户的积分加1
$User->where(&#39;id=5&#39;)->save($data); // 根据条件保存修改的数据
Copy after login

快捷查询

where方法支持快捷查询方式,可以进一步简化查询条件的写法,例如:

一、实现不同字段相同的查询条件

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

查询条件就变成 name= 'thinkphp' OR title = 'thinkphp'

二、实现不同字段不同的查询条件

$User = M("User"); // 实例化User对象
$map[&#39;status&title&#39;] =array(&#39;1&#39;,&#39;thinkphp&#39;,&#39;_multi&#39;=>true);
// 把查询条件传入查询方法
$User->where($map)->select();
Copy after login

'_multi'=>true必须加在数组的最后,表示当前是多条件匹配,这样查询条件就变成 status= 1 AND title = 'thinkphp' ,查询字段支持更多的,例如:

$map[&#39;status&score&title&#39;] =array(&#39;1&#39;,array(&#39;gt&#39;,&#39;0&#39;),&#39;thinkphp&#39;,&#39;_multi&#39;=>true);
Copy after login

查询条件就变成 status= 1 AND score >0 AND title = 'thinkphp'

注意:快捷查询方式中“|”和“&”不能同时使用。

区间查询

where方法支持对某个字段的区间查询,例如:

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

得到的查询条件是: (`id` > 1) AND (`id` < 10)

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

得到的查询条件是: (`id` > 3) OR (`id` < 10)

$map['id'] = array(array('neq',6),array('gt',3),'and');

得到的查询条件是:(`id` != 6) AND (`id` > 3)
最后一个可以是AND、 OR或者 XOR运算符,如果不写,默认是AND运算。

区间查询的条件可以支持普通查询的所有表达式,也就是说类似LIKE、GT和EXP这样的表达式都可以支持。另外区间查询还可以支持更多的条件,只要是针对一个字段的条件都可以写到一起,例如:

$map[&#39;name&#39;] = array(array(&#39;like&#39;,&#39;%a%&#39;), array(&#39;like&#39;,&#39;%b%&#39;), array(&#39;like&#39;,&#39;%c%&#39;), &#39;ThinkPHP&#39;,&#39;or&#39;);
Copy after login

最后的查询条件是:

(`name` LIKE &#39;%a%&#39;) OR (`name` LIKE &#39;%b%&#39;) OR (`name` LIKE &#39;%c%&#39;) OR (`name` = &#39;ThinkPHP&#39;)
Copy after login

组合查询

组合查询用于复杂的查询条件,如果你需要在查询的时候同时偶尔使用字符串却又不希望丢失数组方式的灵活的话,可以考虑使用组合查询。

组合查询的主体还是采用数组方式查询,只是加入了一些特殊的查询支持,包括字符串模式查询(_string)、复合查询(_complex)、请求字符串查询(_query),混合查询中的特殊查询每次查询只能定义一个,由于采用数组的索引方式,索引相同的特殊查询会被覆盖。

一、字符串模式查询(采用_string 作为查询条件)

数组条件还可以和字符串条件混合使用,例如:

$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&#39;;
$User->where($map)->select();
Copy after login

最后得到的查询条件就成了:

( `id` != 1 ) AND ( `name` = &#39;ok&#39; ) AND ( status=1 AND score>10 )
Copy after login

二、请求字符串查询方式

请求字符串查询是一种类似于URL传参的方式,可以支持简单的条件相等判断。

$map[&#39;id&#39;] = array(&#39;gt&#39;,&#39;100&#39;);
$map[&#39;_query&#39;] = &#39;status=1&score=100&_logic=or&#39;;
Copy after login

得到的查询条件是:`id`>100 AND (`status` = '1' OR `score` = '100')

三、复合查询

复合查询相当于封装了一个新的查询条件,然后并入原来的查询条件之中,所以可以完成比较复杂的查询条件组装。
例如:

$where[&#39;name&#39;] = array(&#39;like&#39;, &#39;%thinkphp%&#39;);
$where[&#39;title&#39;] = array(&#39;like&#39;,&#39;%thinkphp%&#39;);
$where[&#39;_logic&#39;] = &#39;or&#39;;
$map[&#39;_complex&#39;] = $where;
$map[&#39;id&#39;] = array(&#39;gt&#39;,1);
Copy after login

查询条件是

( id > 1) AND ( ( name like &#39;%thinkphp%&#39;) OR ( title like &#39;%thinkphp%&#39;) )
Copy after login

复合查询使用了_complex作为子查询条件来定义,配合之前的查询方式,可以非常灵活的制定更加复杂的查询条件。

很多查询方式可以相互转换,例如上面的查询条件可以改成:

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

最后生成的SQL语句是一致的。

多次调用

3.1.3版本开始,where方法支持多次调用,但字符串条件只能出现一次,例如:

$map[&#39;a&#39;] = array(&#39;gt&#39;,1);
$where[&#39;b&#39;] = 1;
$Model->where($map)->where($where)->where(&#39;status=1&#39;)->select();
Copy after login

多次的数组条件表达式会最终合并,但字符串条件则只支持一次。

关于ThinkPHP中where()方法的使用,本文就介绍这么多,希望对您有所帮助,谢谢!

相关推荐:

ThinkPHP中redirect用法分析

The above is the detailed content of Detailed explanation of where() usage in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template