create table orders
(
customerid int NOT NULL,
orderid int NOT NULL,
discount_code int NULL
);
create table discounts
(
id int NOT NULL,
value int NOT NULL
);
create table customers
(
id int not null,
name varchar(20) not null,
phone varchar(20) not null
);
create table orderitems
(
orderid int NOT NULL,
productid int NOT NULL
);
create table products
(
productid int not null,
name varchar(20) not null,
productprice int not null
);
insert into customers
values
(1, "TEST NAME", "TEST PHONE");
insert into orders
values
(1, 1, 2),
(2, 1, null);
insert into discounts
values
(1, 3),
(2, 3);
insert into orderitems
values
(1, 1),
(1, 1);
insert into products
values
(1, "Product Name", 39);
SELECT sum(if(orders.discount_code is not null, products.productprice - discounts.value, products.productprice)) as fullprice, customers.name, customers.phone
FROM `customers`
inner join orders on orders.customerid = customers.id
inner join discounts on orders.discount_code = discounts.id
inner join orderitems on orderitems.orderid = orders.orderid
inner join products on products.productid = orderitems.productid WHERE customers.id = "1";