Mysql
 sql >> Base de données >  >> RDS >> Mysql

SQL LIKE % dans le tableau

$sql = array('0'); // Stop errors when $words is empty

foreach($words as $word){
    $sql[] = 'name LIKE %'.$word.'%'
}

$sql = 'SELECT * FROM users WHERE '.implode(" OR ", $sql);

Edit :code pour CakePHP :

foreach($words as $word){
    $sql[] = array('Model.name LIKE' => '%'.$word.'%');
}

$this->Model->find('all', array(
    'conditions' => array(
        'OR' => $sql
    )
));

Lisez à ce sujet :http://book.cakephp .org/1.3/en/view/1030/Complex-Find-Conditions