How to write Zend db select with OR condition inside AND condition

Can someone help me write a query like the one below using Zend db select:

SELECT `tbl_md_users`.* FROM `tbl_md_users` WHERE user_type <> 'TYPE1') AND (first_name LIKE '%tom%' OR last_name LIKE '%tom%' OR user_name LIKE '%tom%') 
+6
source share
4 answers

If you want to

 SELECT `tbl_md_users`.* FROM `tbl_md_users` WHERE user_type <> 'TYPE1') AND (first_name LIKE '%tom%' OR first_name LIKE '%dick%' OR first_name LIKE '%harry%') 

then the first answer does not work

Instead, I used Zend_Db_Expr

 $likeTom = new Zend_Db_Expr("first_name LIKE '%tom%'"); $likeDick = new Zend_Db_Expr("first_name LIKE '%dick%'"); $likeHarry = new Zend_Db_Expr("first_name LIKE '%harry%'"); $query = $database->select () ->from ('tbl_md_users') ->where ('user_type <> ?', 'TYPE1') ->where ("{$likeTom} OR {$likeDick} OR {$likeHarry}"); 
+5
source
  $query = $database->select () ->from ('tbl_md_users') ->where ('user_type <> ?', 'TYPE1') ->where ("first_name LIKE '%?%' OR last_name LIKE '%?%' OR user_name LIKE '%?%'", 'tom'); 
+4
source

The current, current solution is to call nest () and unnest () in the where clause:

 $select->where ->nest() ->equalTo('column1', 1) ->or ->equalTo('column2', 2) ->unnest() ->and ->equalTo('column3', 3); 
+1
source

I think that Zend_Db_Select cannot be used for this. you can use

 $this->table->getAdapter()->quoteInto() 

To write a user request.

0
source

All Articles