Can someone guide me to write a query like 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%')
If you wanted
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 doesn't work
I used Zend_Db_Expr instead
$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}");
$query = $database->select ()
->from ('tbl_md_users')
->where ('user_type <> ?', 'TYPE1')
->where ("first_name LIKE '%?%' OR last_name LIKE '%?%' OR user_name LIKE '%?%'", 'tom');
The current, up-to-date 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With