call create_order_infos(2,3,300);
delimiter $$
create PROCEDURE `create_order_infos`(
In `in_user_id` int,
In `in_product_id` int,
In `in_count` INT)
BEGIN
# 定义总价格
declare all_price decimal(10,2);
declare product_price decimal(10,2);
declare count_money decimal(10,2);
declare result_str varchar(200);单独定义一个变量用于字符串拼接
declare last_order_id_info int; 最后插入的ID号码
# 需要计算购买产品所需的金额, 用户钱包是否足够支付
select price into product_price from product where product_id=in_product_id;
set all_price = product_price * in_count;
select balance into count_money from user_wallte where user_id=in_user_id;
if all_price>count_money then
set result_str=concat('金额低于',all_price,'无法下单'); 就可一使用变量与字符串进行拼接
signal sqlstate '45000' set message_text=result_str; 相当于result结束
end if;
进入数据库语句操作
1.修改商品数量,减去购买数量
update product set stock=stock-3 where product_id=in_product_id;
2.扣除用户购买商品的金额
update user_wallet set balance=balance-all_price where user_id=in_user_id;
3.添加各种日志预订单信息
insert into user_wallet_log(user_id,order_status,payment_status,payment_time,total_price)values(in_user_id,'待支付','未支付',now(),all_price);
set last_order_id_info=LAST_INSERT_ID();
5.添加订单信息详情表
insert into `order_info` (order_id,prouduct_id,quantity,unit_price)values(last_order_id_info,in_product_id,in_count,product_price
);
END $$
delimiter ;
My sql 数据库查询商品可买与否
于 2024-06-20 15:21:33 首次发布