I have created a new table in Magento 1 like this

enter image description here

How to join table sales_order and custom_table where increment_id = order_number. I try this code but it did not work for me, it hasn't data and shows error


Integrity constraint violation: 1052 Column '`created_at`' in where clause is ambiguous

My Code

    $tbl_faq_item = Mage::getSingleton('core/resource')->getTableName('custom_table');
    $ordershippeddays = Mage::getStoreConfig('custom/customgroup/x_day');
    $fromDate         = gmdate("Y-m-d H:i:s", gmmktime(0, 0, 0, gmdate("m"), gmdate("d") - $ordershippeddays, gmdate("Y")));
    $toDate           = gmdate("Y-m-d H:i:s", gmmktime(23, 59, 59, gmdate("m"), gmdate("d"), gmdate("Y")));
    $collection = Mage::getModel('sales/order')->getCollection();
    $collection->addFieldToFilter('customer_id', array('eq'=> $this->getCustomerLogin()->getId()));
    $collection->addAttributeToFilter('created_at', array('from' => $fromDate, 'to' => $toDate));
    $collection->addFieldToFilter('status', array('in' => array('complete','processing')));
       ->join(array('t2' => $tbl_faq_item),'main_table.increment_id = t2.order_number')->where('main_table.increment_id = t2.order_number');


Column 'created_at' in where clause is ambiguous means your both table has column created_at and when you are applying filter SQL does not understand which table's created_at column to use.

Update this line

$collection->addAttributeToFilter('created_at', array('from' => $fromDate, 'to' => $toDate));


$collection->addAttributeToFilter('main_table.created_at', array('from' => $fromDate, 'to' => $toDate));

Hope this helps you.

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.