0

I need magento sql collection. Query need like that.
select * from custom_table where customer_id=xx or (customer_group=1 and custom_field=1)

I can add or operator like below
select * from custom_table where customer_id=xx or (customer_group=1) But needs to and operator with second OR.

My Current collection:

   $collection = $this->customTableFactory->create()->getCollection()->addFieldToFilter(
       ['customer_id','custom_field'],
   [
       ['eq' =>$customerId],
       ['eq' =>1]
   ]);

Query From above collection:
SELECT main_table.* FROM custom_table AS main_table WHERE ((customer_id = 'xx') OR (custom_field = 1))

But I want collection like as below: Need to add And condition in second OR
SELECT main_table.* FROM custom_table AS main_table WHERE ((customer_id = 'xx') OR (custom_field = 1 and customer_group=1))

Please help me if anyone get solution.

Thanks!

2 Answers 2

1

Can do it as follows

$collection = $this->customTableFactory->create()->getCollection();
$collection->getSelect()->where("customer_group = '1' AND custom_field = '1'")->orWhere("customer_id='xx'");

0

You can add your combined condition using the where function of the collection's select object. Something like this should work for you

$collection = $this->customTableFactory->create()->getCollection();
$collection->addFieldToFilter('customer_id',$customerId);
$collection->getSelect()->where('custom_field = 1 AND customer_group = 1');
1
  • Query Returning: SELECT main_table.* FROM shipping_account AS main_table WHERE (customer_id = '27') AND (share_account = 1 AND customer_group = '1'). Dont need And operator between. Need Or. Posted answer on Query. Thanks!
    – Nadim
    Jun 15, 2020 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.