Mackenzie Jansson
Mackenzie Jansson

Reputation: 3

Multiplying values in SELECT statement without a FROM clause

I'm stuck on my homework problem for so many hours. With this problem, I have to use multiplication in MySQL with a SELECT statement and no FROM statements.

These are what I need to make into columns

price = 100

tax_rate = .07

tax_amount = price * tax_rate

total = price + tax_amount

I've tried to put it all in one line of code, but I get an error that it can't read price and tax_rate when I use them in tax_amount.

It should have columns like this

price = 100

tax_rate = .07

tax_amount = 7

total = 107

EDIT: It was just using aliases. Thanks for the help! I thought it would've been more complex than that given my previous answers on the homework.

Upvotes: 0

Views: 106

Answers (4)

Sushant Sharma
Sushant Sharma

Reputation: 86

This can be done using a basic 'Stored Procedure', without using FROM statement. This gives the result exactly the way you want

Here is how you can do it

DROP PROCEDURE IF EXISTS Variable1;

delimiter //
CREATE PROCEDURE Assignment()                
     BEGIN    
     DECLARE  price  INT ;
     DECLARE tax_rate FLOAT;
     DECLARE tax_amount FLOAT;
     DECLARE total FLOAT;

     SET price = 100;
     SET tax_rate = 0.07;
     SET tax_amount = price * tax_rate;
     SET total = price + tax_amount;

    # SELECT concat('price = ', price ), concat('tax_rate',tax_rate), concat('tax_amount =',price * tax_rate), concat('total =',price + tax_amount) ; 
    SELECT concat('price =',price) as Result
    UNION
    SELECT concat('tax_rate = ',tax_rate)
    UNION
    SELECT concat('tax_amount =',tax_amount)
    UNION
    SELECT concat('total = ',total);    
     END
    //

call Assignment();

Result
price = 100
tax_rate = 0.07
tax_amount = 7
total = 107

Please mark the answer as solved, if this helps you

Upvotes: 0

P.Salmon
P.Salmon

Reputation: 17655

I guess this a homework question about the use of aliases. Select 100 as price,.07 as tax_rate, 100 * .07 as tax_amount ..etc ;

Upvotes: 0

linh nguyenvan
linh nguyenvan

Reputation: 121

I hope it could help you.

MYSQL Arithmetic Operators:

https://dev.mysql.com/doc/refman/8.0/en/arithmetic-functions.html

Upvotes: 0

Arulkumar
Arulkumar

Reputation: 13237

I hope you are looking for:

SELECT price + (price * tax_rate) AS total 

Upvotes: 1

Related Questions