我们正在开发库存跟踪系统。基本上我们有一个order
表,其中下了订单。付款后,状态会从0
更改为1
。此表在另一个表order_items
中有多个子项。
这是主要结构。
CREATE TABLE order(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user_id INT UNSIGNED,
status INT(1),
total INT UNSIGNED
);
CREATE TABLE order_items(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
order_id INT UNSIGNED,
article_id INT UNSIGNED,
size enum('s', 'm', 'l', 'xl'),
quantity INT UNSIGNED
);
现在,我们有一个stocks
表,其中包含类似的收购架构。这是结构。
CREATE TABLE stock(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
article_id INT UNSIGNED
);
CREATE TABLE stock_items(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
stock_id INT UNSIGNED,
size enum('s', 'm', 'l', 'xl'),
quantity INT(2)
);
主要区别在于stocks
没有status
字段。 我们正在寻找的方法是将每篇文章的大小与stock_items
相加,然后将每篇文章的大小与order_items
Order.status = 1
相加并减去这两项以查找我们当前的广告资源
这是我们想要从单个查询获得的表:
Size | Stocks | Sales | Available
s | 10 | 3 | 7
m | 15 | 13 | 2
l | 7 | 4 | 3
最初我们考虑使用complex find conditions,但也许这是错误的方法。
此外,由于它不是直接连接,因此结果非常困难。
这是我们必须检索每个项目的股票总数的代码。
function stocks_total($id){
$find = $this->StockItem->find('all', array(
'conditions' => array(
'StockItem.stock_id' => $this->find('list', array('conditions' => array('Stock.article_id' => $id)))
),
'fields' => array_merge(
array(
'SUM(StockItem.cantidad) as total'
),
array_keys($this->StockItem->_schema)
),
'group' => 'StockItem.size',
'order' => 'FIELD(StockItem.size, \'s\', \'m\' ,\'l\' ,\'xl\') ASC'
));
return $find;
}
感谢。
答案 0 :(得分:0)
当遇到更复杂的多步问题时,我可能会尝试使用临时表。
所以:
使用stock_items中的值集创建一个临时表,然后根据您与order_items的链接逻辑更新该表。
答案 1 :(得分:0)
实际上,我认为这是不可能的。我最终做的是查询两个表,然后用PHP进行计算。