0

I have created a sql query but i don't know how to make this as in the Magento2 format any help

$sql = "`select product_id,customer_Id,bid_status,MAX(bid_Amount) max_bid FROM mbid_history as d where customer_id in(select max(customer_id) from mbid_history as m where m.product_id=d.product_id) AND bid_status=1 group by product_id,customer_id,bid_status`";

I have tried this but it is returns wrong data

$sql = $connection->select()
            ->from(
                ['o' => $tableName],
                ['MAX(bid_amount) as max_bid', 'product_id', 'customer_id']

            )->where('o.bid_status=?', '1')
            ->group('product_id', 'customer_id');
0

Try this, But i am not sure about it:

$connection = $this->getConnection();

  $subSelect = $connection->select()->from(
    ['m' => 'mbid_history'],
    [ new \Zend_Db_Expr('MAX(customer_id)')]
)->where(
    'm.product_id = d.product_id'
);

$select = $connection->select()->from(
    'd' =>'mbid_history',
    ['max_bid' => new \Zend_Db_Expr('MAX(bid_Amount)'), 'product_id', 'customer_Id','bid_status']
)->where('bid_status=?', '1'

)->where('customer_id IN (?)', new \Zend_Db_Expr(sprintf('(%s)', $subSelect->assemble())))

->group(
    ['product_id', 'customer_id' ,'bid_status']
);
  • Tried this but not working. – Naveenbos Apr 20 '17 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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