Mega Code Archive

 
Categories / MySQL / Select Clause
 

SQL variables may be used to store the results of intermediate calculations

mysql> mysql> SET @daily_room_charge = 100.00; Query OK, 0 rows affected (0.00 sec) mysql> SET @num_of_nights = 3; Query OK, 0 rows affected (0.00 sec) mysql> SET @tax_percent = 8; Query OK, 0 rows affected (0.00 sec) mysql> SET @total_room_charge = @daily_room_charge * @num_of_nights; Query OK, 0 rows affected (0.00 sec) mysql> SET @tax = (@total_room_charge * @tax_percent) / 100; Query OK, 0 rows affected (0.00 sec) mysql> SET @total = @total_room_charge + @tax; Query OK, 0 rows affected (0.00 sec) mysql> SELECT @total; +---------------+ | @total        | +---------------+ | 324.000000000 | +---------------+ 1 row in set (0.00 sec) mysql>