I am working on Magento 2 grid UI component. For that I am generating grid using multiple table which does not have any relation( so I cant join). I am using union to combine the tables. But while trying to use it giving following error.error image

This is my code:

protected function _initSelect()

    $sqlq=clone $this->getSelect();
    $sqlT=$this->getSelect()->reset()->from('procurement_owned', ['created_at'=>'created_at']);                                                      
    $sql2= clone $sqlT;     
    $this->getSelect()->reset()->union([$sqlq,$sql2], \Zend_Db_Select::SQL_UNION_ALL);

    return $this;

Then I printed the sql query run that directly into phpmyadmin its giving expected output by combining two tables.

I debugged little and found following line form Zend_Db_Select class throwing the error

public function columns($cols = '*', $correlationName = null)
    if ($correlationName === null && count($this->_parts[self::FROM]))  {
        $correlationNameKeys = array_keys($this->_parts[self::FROM]);
        $correlationName = current($correlationNameKeys);

    if (!array_key_exists($correlationName, $this->_parts[self::FROM]){
        * @see Zend_Db_Select_Exception
        #require_once 'Zend/Db/Select/Exception.php';
        throw new Zend_Db_Select_Exception("No table has been specified for the FROM clause");
    $this->_tableCols($correlationName, $cols);

    return $this;

i don't know what cause the error. please help me. Thanks

  • Is there any specific table from the first $this->getSelect()->reset? – Khoa TruongDinh Dec 6 '16 at 16:21
  • Yes. but am resetting the columns alone. – Krishnan Daiva Dec 6 '16 at 18:33
  • Can you share me the full code please, even I am having the same problem. – Amy Jan 22 '19 at 9:42

This is problem with getting count form the union query

i referred the following link and fixed the issue.



Then i overridden the getSelectCountSql from Magento\Framework\Data\Collection\AbstractDb to get count from union query

  public function getSelectCountSql()
        $unionSelect = clone $this->getSelect(); 
        $countSelect = clone $this->getSelect(); 

      return $countSelect;

Hope someone can use this method.

  • I was implementing a custom filter in the new (2.3.x) order grid to optionally include archived orders. I decided to utilize a UNION and ran into this same issue - thank you very much 🙏🏼 – Logan Jan 17 at 4:42

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.