In my product page Price sorting not works properly. So i need to know which field value consider for sorting ? the issue occurs in configurable products.How do I print the actual MySQL code that gets executed?


Let's say you have a product collection like this:

//$collection is instance of Magento\Catalog\Model\ResourceModel\Product\Collection
                'status', array('eq' => \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)

Then you can simple get sql query by this:

echo $collection->getSelect();


you can add this to the app/code/Magento/Catalog/view/frontend/templates/product/list.phtml file: (File location would depend on your theme)

echo $_productCollection->getSelect();

Try this type code that is example of how to print mysql query.

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $productCollections = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collections = $productCollections->create();
    echo $collections->getSelect()->__toString();


In a simple way, echo your collection with getSelect function.

Here it is

echo $collection->getSelect();

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.