Description of the in operator of the where method of ThinkPHP

In general, the use of the in operator in a SQL statement is as follows:

select * from `table1` where `id` in (1,2,3);

So as you can see, the in operator is followed by a pair of parentheses. The scope of in is enclosed in parentheses.


In ThinkPHP, the in operation is written as follows:
$where = array();
$where['id'] = array('in','1,2,3');
M('table1')->where( $where)->select();

Note:

1. Do not enclose '1, 2, 3' in parentheses after in, ThinkPHP will automatically add parentheses during the process of converting into SQL statements.

2. Because the range of in is a number, you can directly write '1,2,3'. If it is a string, you need to use an array to pass parameters. Such as:
$list = array('a','b','c');
$where['type'] = array('in',$list);

To sum up, if your scope variable is an array , then put it directly into the condition. If your scope variables are concatenated by strings, remember not to put parentheses around them.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325236005&siteId=291194637