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;

to

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('User.id IN (1, 2, 3)');

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

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

10 comments baked

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License