magento 内部联接和按日期筛选不起作用,引发违反完整性约束错误

slwdgvem  于 2022-11-12  发布在  其他
关注(0)|答案(2)|浏览(74)

我试图在两个表上进行内部连接以获取数据。但是,当我搜索以从表中获取日期时,它成功了。我收到一个错误消息:

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'));
b4lqfgs4

b4lqfgs41#

你可以试试这样的

$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会更好
你到底想通过选择得到什么?
请描述您的任务。

j0pj023g

j0pj023g2#

解决了。

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

相关问题