我有 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)
What I wanted to do was update the fields quantity_on_hand quantity_allocated quantity_on_order
What I want the outcome to be is: When a record is added to sales_order_parts table, (creating a new item for a sales order) add the order_quantity to quantity_allocated field in master_part_list. When a record is changed or deleted for sales_order_parts for the order_quantity, update the quantity_allocated field. When a record is invoices, as indicated by a value in the invoice_number field, deduct the ship_quantity from the quantity_on_hand. Basic inventory management.
I was wrote a trigger for Insert on sales_order_parts create trigger allocated_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)
这是有效的,在每条新记录之后,代码运行并更新 quantity_allocated 为 null invoice_number(表示尚未开具发票),总计按部分分组。
我正在处理 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-(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 不为空的情况下它不起作用。基本上,当订单开具发票时,从 master_part_list 中的 quantity_on_hand 中减去 ship_quantity 的数量,但只减去该特定销售订单的零件。我收到错误:
#1054 - 'Unknown column 'new.sales_orders.invoice_number' in 'where clause"
有什么想法吗?
最佳答案
经过一番努力,我想通了。
下面是插入 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 ;
这些触发器脚本对我来说效果很好,但欢迎提出任何建议或意见。谢谢。
关于mysql - 更新 mysql 中的表后触发错误 1054,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54204023/