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

CakePHP怎么写MySQL的IN条件

WBOY
Release: 2016-06-06 20:50:49
Original
1106 people have browsed it

我的代码是这么写的

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

拼出来的SQL是

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

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

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

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

回复内容:

我的代码是这么写的

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

拼出来的SQL是

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

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

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

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

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

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

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

示例如下 ...

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