有三张表:
tbl_order
id 订单ID
uid 用户ID
need_credit 订单所需积分
status 订单状态
....
tbl_coupon
id
uid
used_order 优惠码所使用到的order.id
used 0:未使用/1:已使用/2:已标记为不可用
....
tbl_member
id 用户ID
credit 用户积分
....
条件:传入$order_id
把tbl_order中
id = {$order_id}
的status标记为2
把tbl_coupon中
used = 2 AND used_order = {$order_id}
的 used标记为0, used_order 标记为 ""
把tbl_member中
id = tbl_order.uid /* 这里的tbl_order.uid是上面tbl_order.id={$order_id}那条数据的tbl_order.uid */
的 credit += tbl_order.need_credit
需要由一句SQL来完成
请教了老师,用储存过程来分步实现了这个需求。
存储过程就是将若干sql封装成的一个用来调用的“函数”
虽然已经可以暂时告一段落,仍然期待着更完美的方案...
Tested:
I have a question. Is the logic of the above
mysql
statement the user's order cancellation action? Restore the used coupons, return the user's used points, and then modify the order status.What is easy to overlook is that all the tables to be used should be introduced in the very beginning
update
.Personally I think the answer given by the teacher is not very reliable
There is nothing wrong with using stored procedures for this thing, but as we all know, stored procedures consume MySQL resources and are difficult to maintain
What you describe is actually a complete transaction,
Use mysql transactions to implement it
I don’t know which programming language you are using, but there should be methods that support transactions.