Home >Backend Development >PHP Tutorial >CakePHP怎么写MySQL的IN条件

CakePHP怎么写MySQL的IN条件

WBOY
WBOYOriginal
2016-06-06 20:50:491080browse

我的代码是这么写的

$ids = '1,2,3,4';
$conditions[]=array('id IN (?)'=> $ids);

拼出来的SQL是

SELECT ...ooxx... WHERE id IN ('1,2,3,4');

这条SQL其实是错的,应该是

SELECT ...ooxx... WHERE id IN (1,2,3,4);

我应该在代码里怎么写呢?

回复内容:

我的代码是这么写的

$ids = '1,2,3,4';
$conditions[]=array('id IN (?)'=> $ids);

拼出来的SQL是

SELECT ...ooxx... WHERE id IN ('1,2,3,4');

这条SQL其实是错的,应该是

SELECT ...ooxx... WHERE id IN (1,2,3,4);

我应该在代码里怎么写呢?

$conditions[] = array(
                    'id' => array(1, 2, 3, 4 )
                );

可以通过在对应的字段名后面设置一个包含有值的数组来实现与SQL逻辑运算符IN()同等的效果。

久不用了 ... 在我记忆中直接传递数组即可 ...

示例如下 ...

$this->foo->find( 'all', array(
    'conditions' => array( 'foo.bar' => array( 1, 2, 3, 4 ) )
) );
Statement:
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