7

I'm using below code

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$select = $connection->select()
        ->from(
        ['ce' => 'customer_entity']
);
$data = $connection->fetchAll($select);

It's working fine. It returns all customer fields. I would like to select only First Name, Last Name & Email.

Instead of select(), which function we need to use & how?

3 Answers 3

16

Try following way:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$select = $connection->select()
    ->from(
        ['ce' => 'customer_entity'],
        ['firstname', 'lastname', 'email']
    );
$data = $connection->fetchAll($select);
3
  • 1
    How will we add where condition in this? May 22, 2018 at 6:09
  • 1
    @AmritPalSingh - Taking reference from Sohel Rana's query after select statement(& before fetchAll) you can add where clause ie. $select->where('customer_id = ?', (int)$cust_id); Nov 26, 2018 at 6:31
  • How can you make the select distinct?
    – Black
    Dec 2, 2022 at 15:14
3

Use below code for select particular field from database table.

File path: magento/app/code/Venodr/ModuleName/Model/CustomerData.php

<?php

namespace Venodr\ModuleName\Model;

use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\EntityManager\Operation\ExtensionInterface;

class CustomerData
{
    protected $connection;

    public function __construct(
        \Magento\Framework\App\ResourceConnection $resource
    )
    {
        $this->connection = $resource->getConnection();
    }

    public function getQueryData()
    { 
        $query = $this->connection->fetchAll("SELECT firstname, lastname, email FROM customer_entity");
        return $query;
    }
}
0
$select = $adapter->select()
    ->from(
        ['c' => $tableName],
        ['*','total' => new \Zend_Db_Expr('c.qty + c.sale_qty')]
    );
1
  • 1
    Self blog promotion is not allowed Oct 19, 2023 at 18:47

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.