Using an IN list in a condition

Published on and tagged with cakephp  sql  tip

In SQL there exists a nice concept called IN list that allows you to rewrite an SQL statement like

SELECT x FROM y WHERE x = 1 OR x = 2 OR x = 3;


SELECT x FROM y WHERE x IN (1, 2, 3);

If you want to use this concept in CakePHP, you have two possibilities. You can use the string syntax:

$this->User->findAll(' IN (1, 2, 3)');

Or you can use the array syntax which generates the IN list automatically:

$this->User->findAll(array('' => array(1, 2, 3)));

10 comments baked

© daniel hofstetter. Licensed under a Creative Commons License