Good day / Night!
I am getting ready for last test which will be tomorrow. Since I have idea, what will be on test, I wanted to ask for your help on subject(Procedures). (They are my weak spot).
Here are my tables:
Discount_cards
Create table Discount_cards
(
Card_code Int Auto_Increment Primary key,
Number char(9),
Sum Decimal(10,2),
Discount Int
);
Purchases
Create table Purchases
(
SellCode Int Auto_Increment Primary key,
Date date,
Quantity Int,
PricePerUnit Decimal(10,2),
Discount Int,
Foreign key (Discount) references Discount_cards(Card_code) On delete cascade
);
So, here is my problem.
Basically, we know how my table.Purchases looks like. (I mean colon names.)
Now, I need to create procedure which will SELECT [Date, PricePerUnit, Quantity] where year(Date) = 2015.
Problem
It would be very easy to just select all of these columns, but that would be too easy right?
Here is how it would look like:
delimiter //
CREATE PROCEDURE procedure1 (OUT X INT)
BEGIN
Select 'Date', 'Quantity', 'PricePerUnit' from Purchases where year(Date) = 2015;
END//
delimiter ;
CALL procedure1(@z);
Select @z;
Since that's just too easy, I need to Select this same columns, but rename them, and add Total.
Here is how it should look like:
delimiter //
CREATE PROCEDURE procedure2 (OUT X INT)
BEGIN
Select 'Date' as Purchase_date where year(date) = 2015; *Here is problem*
END//
delimiter ;
CALL procedure2(@z);
Select @z;
So yeah... here is my problem. I know how to Select date and rename it as Purchase_date, but how do I Select multiple Colons with their custom names?
And last but not least, How to add Total in this procedure.
Thank you for your time.