Magento 2:从集合查询中选择字段


8

我有以下查询。我只想从表中选择1个字段,而不是全部。

$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;

我只想id从表中选择字段。

Answers:



24

您需要首先重置所有列,然后选择特定的列。请尝试以下方式:

$ collection-> getSelect()
            ->重置(\ Zend_Db_Select :: COLUMNS)
            ->列(['id']);

这个工作了。谢谢!
阿姆里特·帕·辛格

0

使用此查询。

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

其中detail是表名的别名,推荐是col的名称

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.