1. Query methods
ThinkPHP provides three basic query methods: string condition query, index array condition query and object item query. In most cases, it is recommended to use index arrays and objects as query conditions, because it is safer1. Use strings as condition queries
//Strings as condition queries
$user = M('User');
var_dump($user->where('id=1 AND user="Crayon Shin-chan"')->select());
//Finally generated SQL statement
SELECT * FROM `think_user` WHERE ( id= 1 AND user="Crayon Shin-chan" )
PS: where query method only needs to contain conditions. Multiple conditions can be added with connectors such as AND. We will learn in detail about SQL coherent operations.
2. Use the index array as the query condition
//The index array as the condition query
$user = M('User');
$condition['id'] = 1;
$condition['user'] = 'Crayon Xiaoxin';
var_dump($user->where($condition)->select());
//Finally generated SQL statement
SELECT * FROM `think_user` WHERE ( `id` = 1 ) AND ( `user` = 'Crayon Little
New' )
PS: The default logical relationship of index array query is AND. If you want to change it to OR, you can use _logic to define the query logic.
Add the following line based on the above code:
$condition['_logic'] = 'OR'; //Change the default AND to OR
3. Use the object method to query
//Object as a condition query
$user = M ('User');
$condition = new stdClass();
$condition->id = 1;
$condition->user = 'Crayon Shin-chan';
var_dump($user->where($ condition)->select());
//Finally generated SQL statement
SELECT * FROM `think_user` WHERE ( `id` = 1 ) AND ( `user` = 'Crayon Little
New' )
PS: stdClass Class is a built-in class in PHP, which can be understood as an empty class. Here it can be understood as saving the
field of the condition as a member in the stdClass class. The '' here is to set the namespace to the root directory, otherwise it will cause this class not to be found in the current directory. Using object and array queries, the effect is the same and can be interchanged. In most cases,
ThinkPHP recommends using the array form to be more efficient.
2. Expression query
For those queries that need to achieve fuzzy judgment, such as SQL queries such as greater than, equal to, and less than, you can use the table expression query method.
Query expression format: $map['field name'] = array('expression','query condition');
Expression query table
Expression meaning
EQ is equal to (=)NEQ is not equal to ()
GT greater than (>)
EGT greater than or equal to (>=)
LT less than (ELT less than or equal to ([NOT]LIKE fuzzy query
[NOT] BETWEEN (not here) Interval query
[NOT] IN (not)IN query
EXP expression query, supports SQL syntax
PS: Expressions are not case-sensitive.
//EQ: equal to (=)
$map['id'] = array('eq', 1); //where is id=1
//NEQ: not equal to ()
$map ['id'] = array('neq', 1); //where is id1
//GT: greater than (>)
$map['id'] = array('gt', 1 ); //where is id>1
//EGT: greater than or equal to (>=)
$map['id'] = array('egt', 1); //where is id>=1
// LT: less than ($map['id'] = array('lt', 1); //where is id//ELT: less than or equal to ($map['id '] = array('elt', 1); //where is id//[NOT]LIKE: fuzzy query
$map['user'] = array('like', '%小%' ); //where is like %小%
//[NOT]LIKE: fuzzy query
$map['user'] = array('notlike', '%小%'); //where is not like %小% %
//[NOT]LIKE: array method of fuzzy query
$map['user'] = array('like', array('%小%', '% wax%'), 'AND');
//Generated SQL
SELECT * FROM `think_user` WHERE ( (`user` LIKE '%小%' AND `user`
LIKE '%wax%') )
//[NOT] BETWEEN: interval query
$map ['id'] = array('between','1,3');
//where is `id` BETWEEN '1' AND '2'
//Same as above and equivalent
$map['id'] = array('between',array('1','3'));
//[NOT] BETWEEN: interval query
$map['id'] = array('not between','1,3') ;
//where is `id` NOT BETWEEN '1' AND '2'
//[NOT] IN: interval query
$map['id'] = array('in','1,2,4' );
//where is `id` IN ('1','2','4')
//[NOT] IN: interval query
$map['id'] = array('not in', '1,2,4');
//where is `id` NOT IN ('1','2','4')
//EXP: Custom
$map['id'] = array( 'exp','in (1,2,4)');
//where is `id` NOT IN ('1','2','4')
PS: Use exp customization in the second Just write the where statement directly for the parameters
//EXP: Customize the OR statement
$map['id'] = array('exp', '=1');
$map['user'] = array(' exp', '="Crayon Shin-chan"');
$map['_logic'] = 'OR';
//WHERE is ( (`id` =1) ) OR ( (`user` =" Crayon Shin-chan New") )
Three. Quick query
The shortcut query method is a simplified way of writing multi-field queries. Multiple fields are separated by '|' to represent OR, and '&'
are separated to represent AND.
1. Same query conditions for different fields
//Use the same query conditions
$user = M('User');
$map['user|eemail'] = 'a'; //'|' is replaced with '& 'Becomes AND
var_dump($user->where($map)->select());
2. Different query conditions for different fields
//Use different query conditions
$user = M('User') ;
$map['id&user'] = array(1,'Crayon Shin-chan','_multi'=>true);
var_dump($user->where($map)->select());
PS: Setting '_multi' to true is to make the id correspond to 1 and the user to correspond to 'Crayon Shin-chan'. Otherwise, there will be a situation where id corresponds to 1 and also corresponds to 'Crayon Shin-chan'. Moreover, this setting should be placed at the end of the array.
//Support expressions combined with quick queries
$user = M('User');
$map['id&user'] = array(array('gt', 0),'Crayon Shin-chan','_multi' =>true);
var_dump($user->where($map)->select());
IV. Interval query
ThinkPHP supports interval query for a certain field.
//Interval query$user = M('User');
$map['id'] = array(array('gt', 1), array('lt', 4));
var_dump($ user->where($map)->select());
//The third parameter sets the logical OR
$user = M('User');
$map['id'] = array(array ('gt', 1), array('lt', 4), 'OR');
var_dump($user->where($map)->select());
5. Combined query
Combined query is an expanded query based on the index array query method. It adds string query (_string), complex query (_complex), and request string query (_query). Since it uses an index array, it is repeated. will be overwritten.
//String query (_string)$user = M('User');
$map['id'] = array('eq', 1);
$map['_string'] ='user= "Crayon Shin-chan" AND email="xiaoxin@163.com"';
var_dump($user->where($map)->select());
//Request string query (_query)
$ user = M('User');
$map['id'] = array('eq', 1);
$map['_query'] ='user=Crayon Shin-chan&email=xiaoxin@163.com&_logic= OR';
var_dump($user->where($map)->select());
PS: This method is URL method and does not require quotation marks.
//Composite query (_complex)
$user = M('User');
$where['user'] = array('like', '%小%');
$where['id'] = 1;
$where['_logic'] = 'OR';
$map['_complex'] = $where;
$map['id'] = 3;
$map['_logic'] = 'OR' ;
var_dump($user->where($map)->select());
PS: Compound query can build more complex queries, here id=1 or id=3 can be implemented.
Six. Statistical query
ThinkPHP provides some methods for statistical query of data.
//Total number of data items
$user = M('User');var_dump($user->count());
//Total number of fields, no statistics when NULL is encountered
$user = M( 'User');
var_dump($user->count('email'));
//Maximum value
$user = M('User');
var_dump($user->max('id' ));
//Minimum value
$user = M('User');
var_dump($user->min('id'));
//Average value
$user = M('User') ;
var_dump($user->avg('id'));
//Find the sum
$user = M('User');
var_dump($user->sum('id'));
Seven. Dynamic query
With the features of PHP5 language, ThinkPHP implements dynamic query.
1.getBy dynamic query
//Find the data of email=xiaoin@163.com$user = M('User');
var_dump($user->getByemail('xiaoxin@163.com'));
2.getFieldBy dynamic query
//Get the corresponding id value through user
$user = M('User');
var_dump($user->getFieldByUser('Luffy', 'id'));
eight. SQL query
ThinkPHP supports native SQL query.
1.query reads
//Query result set, if distributed read-write separation is used, it is always executed on the read server$user = M('User');
var_dump($user->query('SELECT * FROM think_user'));
2.execute write
//Update and write, if distributed read and write separation are used, they are always executed on the write server
$user = M('User');
var_dump($user ->execute('UPDATE think_user set user="Crayon Daxin" WHERE
id=1'));
PS: Since the subquery uses a lot of coherent operations, we will explain them in the coherent operations.
The above introduces the ThinkPHP-SQL query statement, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

一、技术背景在实际的项目开发中,我们经常会使用到缓存中间件(如redis、MemCache等)来帮助我们提高系统的可用性和健壮性。但是很多时候如果项目比较简单,就没有必要为了使用缓存而专门引入Redis等等中间件来加重系统的复杂性。那么Java本身有没有好用的轻量级的缓存组件呢。答案当然是有喽,而且方法不止一种。常见的解决方法有:ExpiringMap、LoadingCache及基于HashMap的封装三种。二、技术效果实现缓存的常见功能,如过时删除策略热点数据预热三、ExpiringMap3.

方式1.使用HashtableMaphashtable=newHashtable();这是所有人最先想到的,那为什么它是线程安全的?那就看看它的源码,我们可以看出我们常用的put,get,containsKey等方法都是同步的,所以它是线程安全的publicsynchronizedbooleancontainsKey(Objectkey){Entrytab[]=table;inthash=key.hashCode();intindex=(hash&0x7FFFFFFF)%tab.leng

javabean与map的转换有很多种方式,比如:1、通过ObjectMapper先将bean转换为json,再将json转换为map,但是这种方法比较绕,且效率很低,经测试,循环转换10000个bean,就需要12秒!!!不推荐使用2、通过Java反射,获取bean类的属性和值,再转换到map对应的键值对中,这种方法次之,但稍微有点麻烦3、通过net.sf.cglib.beans.BeanMap类中的方法,这种方式效率极高,它跟第二种方式的区别就是因为使用了缓存,初次创建bean时需要初始化,

标题:C#中使用Array.Sort函数对数组进行排序的示例正文:在C#中,数组是一种常用的数据结构,经常需要对数组进行排序操作。C#提供了Array类,其中有Sort方法可以方便地对数组进行排序。本文将演示如何使用C#中的Array.Sort函数对数组进行排序,并提供具体的代码示例。首先,我们需要了解一下Array.Sort函数的基本用法。Array.So

两种方法:1、利用“for range”语句遍历map来获取全部元素,语法“for key, value := range mapName{...}”。2、使用key做为索引的形式来获取指定元素,语法“value, isOk := mapName[key]”;返回两个返回值,第一个返回值是获取的值,如果key不存在,返回空值,第二个参数是一个bool值,表示获取值是否获取成功。

Laravel集合中的Where方法实用指南在Laravel框架的开发过程中,集合(Collection)是一个非常有用的数据结构,它提供了丰富的方法来操作数据。其中,Where方法是一个常用的筛选方法,能够根据指定条件来过滤集合中的元素。本文将介绍Laravel集合中Where方法的使用,通过具体的代码示例来演示其用法。1.基本用法Where方法的

在进行PHP编程时,我们常常需要对数组进行合并。PHP提供了array_merge()函数来完成数组合并的工作,不过当数组中存在相同的键时,该函数会覆盖原有的值。为了解决这个问题,PHP在语言中还提供了一个array_merge_recursive()函数,该函数可以合并数组并保留相同键的值,使得程序的设计变得更加灵活。array_merge

在Docker中,挂载目录的权限问题通常可以通过以下方法解决:使用-v参数指定挂载目录时添加权限相关的选项。可以通过在挂载的目录后面添加:ro或:rw来指定挂载目录的权限,分别表示只读和读写权限。例如:dockerrun-v/host/path:/container/path:roimage_name在Dockerfile中定义USER指令来指定容器中运行的用户,以确保容器内部的操作符合权限要求。例如:FROMimage_name#CreateanewuserRUNuseradd-ms/bin/


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.