8

I have below query. I want to select only 1 field from table instead of all.

$collection = $this->_collectionFactory->addFieldToFilter('status', 0)->load();
//$collection->getSelect()->column('id');
//$collection->getSelect()->from(['main_table' => $this->getMainTable()], array('main_table.id'));
echo $collectionBallotSelect->getSelect()->__toString();
exit;

I want to select only id field from table.

0

3 Answers 3

5

You can also Try :

$collection = $this->_collectionFactory->create()->addFieldToSelect('id')->addFieldToFilter('status', 0)->load();
30

You need to reset all column first, then select specific column. Try following way:

$collection->getSelect()
            ->reset(\Zend_Db_Select::COLUMNS)
            ->columns(['id']);
1
  • This one worked. Thanks! Commented Mar 27, 2019 at 7:38
1

Use this query.

        $getTotalRecommended = $getCollection ->getSelect()
        ->reset(\Zend_Db_Select::COLUMNS)
        ->columns(['detail.recommended']);

where detail is the alias of the table name and recommended is the name of col

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.