我有3张桌子:
master_parts_list
id int(11) not null auto increment part_number varchar(30) unique description varchar(255) price decimal(10,2) weight decimal(10,2) active tinyint(1) quantity_on_hand decimal(10,2) quantity_allocated decimal(10,2) quantity_on_order decimal(10,2) old_part_number varchar(30)
sales_orders
id int(11) not null primary key auto increment quote_number int(11) unique order_number int(11) unique invoice_number int(11) unique status varchar(255) customer_number int(11) customer_po_number char(30) ...and 15 others...
sales_order_parts
id int(11) not null primary key auto increment order_id int(11) part_id int(5) order_quantity int(5) ship_quantity int(5) unit_price decimal(10,2) note varchar(255)
我想做的就是更新字段 现有数量 数量分配 订购数量
我希望结果是: 将记录添加到sales_order_parts表时(为销售订单创建新项目),将order_quantity添加到master_part_list中的quantity_allocated字段中。当更改或删除了order_quantity的sales_order_parts的记录时,请更新quantity_allocated字段。当记录为发票时,如invoice_number字段中的值所指示,请从订购数量中扣除ship_quantity。基本库存管理。
我在sales_order_parts上被写入了一个插入触发器 创建触发器located_addition_insert
create trigger allocated_addition_insert
after insert on sales_order_parts
for each row
update master_part_list
set master_part_list.quantity_allocated=
(select sum(order_quantity)
from
sales_orders
inner join
sales_order_parts on sales_orders.id = sales_order_parts.order_id
where sales_orders.invoice_number is null and sales_order_parts.part_id = master_part_list.id
group by part_id)
这可行,在每条新记录之后,代码将运行并更新数量为空的invoice_number(表示尚未开具发票)的quantity_allocated,并将总数按部分分组。
我正在为sales_orders的UPDATE触发器
delimiter $$
create trigger allocated_order_on_hand_update
after update on sales_orders
for each row begin
update master_part_list
set master_part_list.quantity_on_hand=master_part_list.quantity_on_hand-(select sum(ship_quantity)
from
sales_orders
join
sales_order_parts on sales_orders.id = sales_order_parts.order_id
where sales_orders.invoice_number = new.sales_orders.invoice_number
and sales_orders.invoice_number is not null
group by part_id);
update master_part_list
set master_part_list.quantity_allocated=(select sum(order_quantity)
from
sales_orders
inner join
sales_order_parts on sales_orders.id = sales_order_parts.order_id
where sales_orders.invoice_number is null and sales_order_parts.part_id = master_part_list.id
group by part_id);
end$$
第二次更新在invoice_number为null时运行,但是当尝试更新并保存在invoice_number字段中设置发票编号的发票时,在invoice_number不为null的情况下不起作用。基本上,当订单被开具发票时,从master_part_list中的amount_on_hand扣除ship_quantity的数量,但只减去该特定销售订单的零件。我收到错误消息:
#1054-“ where子句”中的“未知列'new.sales_orders.invoice_number”
有什么想法吗?
答案 0 :(得分:0)
经过一番工作,我想通了。
这是用于在sales_order_parts上插入的触发器的代码。
它将所有按零件分组的未结订单order_quantity字段的值相加,然后在master_parts_list的quantity_allocated字段中设置值。它不包括所有报价和已开具发票的订单。
create trigger allocated_addition_insert
after insert on sales_order_parts
for each row
update master_part_list
set master_part_list.quantity_allocated=
(select sum(order_quantity)
from
sales_orders
inner join
sales_order_parts on sales_orders.id = sales_order_parts.order_id
where sales_orders.invoice_number is null
and sales_order_parts.part_id = master_part_list.id
and sales_orders.status <> "Quote"
group by part_id);
这是为了更新sales_order_parts中更改数量的值 这是在sales_order_parts上进行更新之前的触发器的代码。
create trigger allocated_minus_update
before update on sales_order_parts
for each row
update master_part_list set master_part_list.quantity_allocated = master_part_list.quantity_allocated - old.order_quantity
where master_part_list.id = old.part_id;
这是sales_order_parts更新后的触发器代码。
create trigger allocated_add_update
after update on sales_order_parts
for each row
update master_part_list set master_part_list.quantity_allocated = master_part_list.quantity_allocated + new.order_quantity
where master_part_list.id = new.part_id;
这是用于sales_orders更新的代码。
delimiter $$
create trigger allocated_order_on_hand_update
after update on sales_orders
for each row begin
update master_part_list
set master_part_list.quantity_on_hand = master_part_list.quantity_on_hand -
coalesce((select sum(ship_quantity)
from
sales_order_parts
inner join
sales_orders on sales_orders.id = sales_order_parts.order_id
where sales_orders.invoice_number = new.invoice_number
and sales_order_parts.part_id = master_part_list.id
and new.invoice_number > 0
group by sales_order_parts.part_id),0);
update master_part_list
set master_part_list.quantity_allocated=
(select sum(order_quantity)
from
sales_orders
inner join
sales_order_parts on sales_orders.id = sales_order_parts.order_id
where sales_orders.invoice_number is null
and sales_order_parts.part_id = master_part_list.id
and sales_orders.status <> "Quote"
group by part_id);
end$$
delimiter ;
这些触发脚本对我来说效果很好,但是欢迎提出任何建议或意见。谢谢。