I have the following query:

SELECT `main_table`.*, `secondTable`.* FROM `table_one` AS `main_table`
LEFT JOIN `table_two` AS `secondTable` 
ON main_table.blog_id = secondTable.blog_id 
WHERE (store_id = '3') OR (store_id = '0');

Now take this query and run in CLI it Works perfect it gives all the data you need but in magento it doens't. I Think there is something preventing it but not sure. So blog_id are in both tables could be why it isn't returning so if it is how could I add alias for the columns blog_id for both tables?



public function getBlogs(sort)
        if (is_null($this->blogs)) {
            $this->blogs = $this->blogCollectionFactory->create()
                ->setOrder(empty($sort)?'position':$sort, 'ASC');
             $this->blogs->getSelect()->reset(\Zend_Db_Select::WHERE)->where('secondTable.store_id = ?', $this->_storeManager->getStore()->getId())->orwhere('secondTable.store_id = ?', 0);
             echo $this->blogs->getSelect();
        return $this->blogs;

enter image description here

So if I don't add the last statement ->orwhere('secondTable.store_id = ?', 0) then it works but I need to return everything including those that belongs to store_id=0 hence why added but in magento doesn't like it;


  • Does store_id column available in both the tables ? – Abdul Pathan Jul 22 at 13:50
  • @ Abdul Pathan No Only second table – Juliano Vargas Jul 22 at 15:44
  • Could you please update the Magento code you are using to run the query. Also show the error it throws if any. – Abdul Pathan Jul 22 at 15:50
  • @AbdulPathan No error the page returns but without items I supposed getCollection() is returning array() – Juliano Vargas Jul 22 at 16:05
  • It would be helpful if you put your Magento code as well in the question. – Abdul Pathan Jul 22 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.