Summary of where conditional query usage in thinkphp 3.2 framework

  • 2021-12-13 16:36:05
  • OfStack

This article illustrates the use of where conditional queries in the thinkphp 3.2 framework. Share it for your reference, as follows:

thinkphp3.2 where conditional query

In the coherent operation, the operation of the condition where is sometimes dizzy, so it is helpful to use it

Query criteria

Supported expression queries, tp case-insensitive

含义 TP运算符 SQL运算符 例子 实际查询条件
等于 EQ = $where['id'] = array('EQ','1') id = 2
不等于 NEQ != $where['id'] = array('NEQ','1') id!=2
大于 GT > $where['id'] = array('GT','1') id >1
大于等于 EGT EGT $where['id'] = array('EGT','1') id>=1
小于 < < $where['id'] = array('lt',1) id < 1
小于等于 <= <= $where['id'] = array('elt',1) id<=1
匹配 like like where[′id′]=array(′like′,′where[′id′]=array(′like′,′where['id'] = array('like','begin%')
$where['id'] = array('like','%begin%')
where id like '%begin'
where id like 'begin%'
where id like'%begin%
在范围内包括俩端值 between 0<=id<=10 $where['id'] = array('between',array('0','10')) where id between 0 and 10
不在范围内 not between 0 >id and 1o < id $where['id'] = array('not between',array('0','10')) where id not between 0 and 10
在枚举的值中 in in $where['id'] = array('in',array('1','2','5')) where id in ('1','2','3')
不在枚举值中 not in not in $where['id'] = array('not in',array('1','2',5)) where id not in ('1','2','5')
exp 表达式查询,支持SQL语法

exp is an expression. If you think there are too many restrictions on 1 value, you can use this


$where['id'] = array('exp','in ( select id from id from tableb)');

Query statement reviewed

Sometimes, we hope to solve the problem through one query. At this time, the query conditions are often complicated, but it is more efficient than multiple queries.

If you can't handle it, use it directly $where['_string'] = 'xxxx' This means that when querying, the xxx condition is spliced to solve the problem once


$where['_string'] = 'left join A on A.id = b.id where a.id not in (select id from C)';

1. Interval query (1 worth multiple cases)

The default is and


$where['id'] =array(array('neq','8'),array('elt','200'),'and'); //  Less than or equal to 200  Not equal to  8
$where['id'] = array(array('neq','8'),'array('neq','10')','or'); //  Not equal to 8 Or not equal to 10

2. Compound query

It is equivalent to encapsulating new query conditions in it


$where['a'] = 5;
$where['b'] = 6;
$where['_logic'] = 'or';

sql: where a = 5 or b = 6;


$condition['c'] = '3';
$condition['d'] = '4'
$condition['_logic'] = 'or'
$where['a'] = 9;
$where['_complex'] = $condition;

sql: where a=9 and (c = 3 or d = 4)

According to the demand, use it flexibly (unlimited set)

3. sql query

If read-write separation is set, query Is a query execute Is the update saved


M()->query('select * from a');
M()->execute('update a set counts = 3 where id = 1103')

4. Get the sql statement to execute

Sometimes the conditions are too complicated, such as id in(xxxxx) This xxx is the result obtained through 1 series of operations. If you are too troublesome, throw it directly. If you write sql long, you will directly get sql statements and throw it in

1.fetchsql
2.buildsql
3.select(false)


M('user')->fetchsql(true)->select();
M('user')->buildsql();
M('user')->select(false);

For more readers interested in thinkPHP related contents, please check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".

I hope this article is helpful to the PHP programming based on ThinkPHP framework.


Related articles: