我有这样的条件,我有付款历史,如果状态仍在等待1天付款,股票将根据购买的产品数量回到第一个值。 我有这样的查询
> CREATE EVENT `stok_sch` ON SCHEDULE
> EVERY 1 SECOND
> DO BEGIN
> UPDATE product a
> SET a.stock = a.stock +
> (select ifnull((select sum(amount) from order_detail x left join order y using (idOrder)
> where y.status='Waiting for payment' and x.idProduct=a.idProduct
> and (date_format(y.order_date,'%Y%m%d')+1 = date_format(now(),'%Y%m%d')+0 or date_format
> (y.order_date,'%Y%m%d')+1 < date_format(now(),'%Y%m%d')+0) group by x.idProduct),0));
>
> UPDATE order set status ='Cancel' where status ='Waiting for payment' and (date_format(order_date,'%Y%m%d')+1 < date_format
>
> (now(),'%Y%m%d')+0 OR date_format(order_date,'%Y%m%d')+1 = date_format(now(),'%Y%m%d')+0);
> END$$;
我更新了我的查询,如果我在没有事件计划的情况下运行它,它可以正常工作,但是当我将它与事件调度程序结合使用时,产品库存从购买量增加了两倍。 任何想法来解决这个问题?
答案 0 :(得分:0)
你可以尝试两件事:
1)将LEFT JOIN
替换为JOIN
,这样您只能获得订单。
2)不要在SET stock = stock + ( ...)
部分中引用子查询中的ab.amount。改为引用x.amount:
查询现在看起来像这样:
UPDATE product AS a, product_detail AS ab, order AS b
SET stock=a.stock +
(select sum(x.amount) as amount
from product_detail x
join order y using (idOrder)
where y.status='waiting for payment'
and x.idProduct = a.idProduct
group by x.idProduct
)
WHERE a.idProduct=ab.idProduct
AND ab.idOrder=b.idOrder
AND b.status='waiting for payment'
and (date_format(order_date,'%Y%m%d')+1 = date_format(now(),'%Y%m%d')+0 or date_format(order_date,'%Y%m%d')+1 < date_format(now(),'%Y%m%d')+0)
;
答案 1 :(得分:0)
使用日期比较而不是转换为数字,也意识到如果有人在晚上10点下订单,这个订单将在午夜后立即取消。
UPDATE product p
JOIN (SELECT
d.idProduct,
SUM(d.amount) amount
FROM
`order` o
JOIN product_detail d
ON o.order_id = d.order_id
WHERE
o.status = 'waiting for payment'
AND
DATE(o.order_date) >= CURRENT_DATE - INTERVAL 1 DAY
GROUP BY idProduct
) pd
ON p.idProduct = pd.idProduct
SET
p.stock = p.stock + pd.amount
WHERE
p.idProduct = pd.idProduct