Zend Framework: How to include an OR statement in an SQL fetchAll()

Posted by Scoobler on Stack Overflow See other posts from Stack Overflow or by Scoobler
Published on 2010-01-18T15:10:16Z Indexed on 2010/03/25 12:13 UTC
Read the original article Hit count: 419

I am trying to build the following SQL statement:

SELECT users_table.*, users_data.first_name, users_data.last_name FROM users_table INNER JOIN users_data ON users_table.id = user_id WHERE (users_table.username LIKE '%sc%') OR (users_data.first_name LIKE '%sc%') OR (users_data.last_name LIKE '%sc%')

I have the following code at the moment:

public function findAllUsersLike($like) { $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART)->setIntegrityCheck(false); $select->where('users_table.username LIKE ?', '%'.$like.'%'); $select->where('users_data.first_name LIKE ?', '%'.$like.'%'); $select->where('users_data.last_name LIKE ?', '%'.$like.'%'); $select->join('users_data', 'users_table.id = user_id', array('first_name', 'last_name')); return $this->fetchAll($select); }

This is close, but not right as it uses AND to add the extra WHERE statements, instead of OR.

Is there any way to do this as one select? Or should I perform 3 selects and combine the results (alot more overhead?)?

P.S. The parameter $like that is past is sanitized so don't need to wory about user input in the code above!

© Stack Overflow or respective owner

Related posts about zend-framework

Related posts about zend-db