suchen

Heim  >  Fragen und Antworten  >  Hauptteil

CakePHP怎么写MySQL的IN条件

我的代码是这么写的

$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);

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

阿神阿神2897 Tage vor460

Antworte allen(2)Ich werde antworten

  • 黄舟

    黄舟2017-04-10 14:26:52

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

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

    Antwort
    0
  • 黄舟

    黄舟2017-04-10 14:26:52

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

    示例如下 ...

    $this->foo->find( 'all', array(
        'conditions' => array( 'foo.bar' => array( 1, 2, 3, 4 ) )
    ) );

    Antwort
    0
  • StornierenAntwort