存储过程MySQL-按下订单数量的会员折扣

时间:2018-11-29 23:50:06

标签: mysql

我正在尝试创建一个存储过程,并根据订单数,客户下的订单将获得特定的折扣。

CREATE TABLE customer (
  cust_ID int NOT NULL AUTO_INCREMENT,
  fname varchar(50) NOT NULL,
  lname varchar(50) NULL,
  DOB date NULL,
  email varchar(50) NULL,
  phone varchar(15) NULL,
  home_addr varchar(200) NULL,
  password varchar(255) NULL,
  PRIMARY KEY (cust_ID)
);

CREATE TABLE cust_order (
  cust_order_ID int NOT NULL AUTO_INCREMENT,
  cust_order_date datetime NOT NULL,
  cust_order_status varchar(50) NOT NULL,
  cust_order_price numeric (5,2) NOT NULL,
  charge_date_time datetime NOT NULL,
  cust_ID int NOT NULL,
  shipping_ID int NOT NULL,
  PRIMARY KEY (cust_order_ID),
  FOREIGN KEY (cust_ID) REFERENCES customer (cust_ID), 
  FOREIGN KEY (shipping_ID) REFERENCES shipment (shipping_ID)
);

CREATE TABLE cust_membership (
  membership_ID int NOT NULL,
  start_date date NOT NULL,
  end_date date,
  cust_ID int NOT NULL,
  PRIMARY KEY (membership_ID, cust_ID), 
  FOREIGN KEY (membership_ID) REFERENCES membership (membership_ID), 
  FOREIGN KEY (cust_ID) REFERENCES customer (cust_ID)
);

CREATE TABLE membership (
  membership_ID int NOT NULL,
  memb_name  varchar(50) NOT NULL,
  discount_value numeric(5,2) NOT NULL,
  PRIMARY KEY (membership_ID)
);

-- STORED PROCEDURE---

DELIMITER //
CREATE PROCEDURE GetMembershipDiscountByOrdersPlaced (IN cust_email varchar(50), OUT cust_memb_name varchar(50), OUT new_order_cost numeric(5,2))
  BEGIN

DECLARE new_order_cost numeric(5,2) DEFAULT 0.00;


SELECT COUNT(*) INTO total_orders 
FROM customer NATURAL JOIN cust_order
WHERE email = cust_email;

SELECT cust_order_price INTO cur_order_cost
FROM customer NATURAL JOIN cust_order
WHERE email = cust_email
ORDER BY cust_order_date DESC
LIMIT 1;


IF total_orders<=15 THEN
    SET new_order_cost = cur_order_cost - (cur_order_cost * 0.05)
    SELECT "Bronze" INTO cust_memb_name;
ELSE 
    IF total_orders<40 THEN
      SET new_order_cost = cur_order_cost - (cur_order_cost * 0.10)
      SELECT "Silver" INTO cust_memb_name;
    ELSE 
      SET new_order_cost = cur_order_cost - (cur_order_cost * 0.15)
      SELECT "Golden" INTO cust_memb_name;
    END IF;
END IF;


  END// 
DELIMITER ; 

你能帮我吗?

0 个答案:

没有答案