簡體   English   中英

內部聯接和按日期過濾不起作用引發完整性約束違反錯誤-Magento 1

[英]Inner Join and filter by date not working throws Integrity constraint violation error - Magento 1

我試圖對兩個表進行內部聯接以獲取數據。 但是當我搜索從表中獲取日期時它起作用了。 我說錯了

Integrity constraint violation: 1052 Column 'created_at' in where clause 
is ambiguous, query was: SELECT COUNT(*) FROM `sales_flat_order` AS 
`main_table`
INNER JOIN `sales_flat_order_item` ON main_table.store_id = 
sales_flat_order_item.store_id WHERE (`created_at` >= '2017-04-01'

這是我的代碼

    $collection = Mage::getModel('sales/order')->getCollection();

    $collection->addFieldToFilter('created_at');
    $collection->getSelect()->join("sales_flat_order_item", 
    "main_table.store_id = sales_flat_order_item.store_id", 
    array('name','sku','order_id','quantity'));

您可以嘗試這樣的事情

$collection = Mage::getModel('sales/order')->getCollection();
$collection->addFieldToFilter('main_table.created_at', '2017-04-01');
$collection->getSelect()->join("sales_flat_order_item",
    "main_table.store_id = sales_flat_order_item.store_id",
    array('name','sku','order_id','qty_ordered'));

但是您的聯接看起來很奇怪,也許最好使用sales_flat_order.entity_id = sales_flat_order_item.order_id然后使用store_id

您到底要從中獲得什么呢?

請描述您的任務。

解決了。

$startDate = date("Y-m-d 00:00:00", strtotime( date( "Y-m-d 00:00:00", 
strtotime( date("Y-m-d 00:00:00") ) ) . "-10 months" ) );
$endDate = date("Y-m-d 00:00:00", strtotime( date( "Y-m-d 00:00:00", 
strtotime( date("Y-m-d 00:00:00") ) ) . "+1 day" ) );
$collection = Mage::getResourceModel('sales/order_item_collection')->addFieldToFilter('created_at', array('from' => $startDate, 'to' => $endDate)->addAttributeToSelect('*');
$this->setCollection($collection);
return parent::_prepareCollection();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM