将查询合并为一个
Combine queries into one
我希望一旦我将产品添加到“订单”table,产品的数量就会减少 1。我写了两个单独的查询来实现这个任务。
向“订单”添加数据的查询table:
INSERT INTO orders(id_buyer, id_product, date, status)
VALUES (2, 8, '2022-05-22', 1);
请求将“数量”减一:
update products
set amount = amount - 1,
amount = amount - 1
where id in ( select id_product from orders where status = 1);
是否可以将这两个查询合并为一个?
在回答问题时(即使这是否正确)您可以使用 AFTER INSERT TRIGGER
尝试:
CREATE TRIGGER after_orders_insert AFTER INSERT ON orders
FOR EACH ROW
UPDATE products
SET amount=amount - 1
WHERE id = NEW.id_product
AND NEW.status =1;
我希望一旦我将产品添加到“订单”table,产品的数量就会减少 1。我写了两个单独的查询来实现这个任务。
向“订单”添加数据的查询table:
INSERT INTO orders(id_buyer, id_product, date, status)
VALUES (2, 8, '2022-05-22', 1);
请求将“数量”减一:
update products
set amount = amount - 1,
amount = amount - 1
where id in ( select id_product from orders where status = 1);
是否可以将这两个查询合并为一个?
在回答问题时(即使这是否正确)您可以使用 AFTER INSERT TRIGGER
尝试:
CREATE TRIGGER after_orders_insert AFTER INSERT ON orders
FOR EACH ROW
UPDATE products
SET amount=amount - 1
WHERE id = NEW.id_product
AND NEW.status =1;