Mega Code Archive

 
Categories / MySQL / Select Clause
 

Calculation

mysql> mysql> CREATE TABLE IF NOT EXISTS wines     -> (     ->   id             INT     AUTO_INCREMENT  PRIMARY KEY,     ->   type           CHAR(10)        NOT NULL,     ->   price          DECIMAL(6,2)    NOT NULL,     ->   quantity       INT             DEFAULT 0     -> ); Query OK, 0 rows affected (0.00 sec) mysql> mysql> INSERT INTO wines (type, price, quantity)   VALUES ("Red", 10.00, 12); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO wines (type, price, quantity)   VALUES ("White", 9.00, 12); Query OK, 1 row affected (0.00 sec) mysql> INSERT INTO wines (type, price, quantity)   VALUES ("Rose", 8.00, 6); Query OK, 1 row affected (0.00 sec) mysql> mysql> SELECT   type,     ->  price AS bottle,     ->  quantity AS qty,     ->  price * quantity AS subtotal,     ->  (price * quantity) * (6 / 100) AS tax,     ->  (price * quantity) +     ->    (price * quantity) * (6 / 100) AS total     -> FROM wines ; +-------+--------+------+----------+----------+------------+ | type  | bottle | qty  | subtotal | tax      | total      | +-------+--------+------+----------+----------+------------+ | Red   |  10.00 |   12 |   120.00 | 7.200000 | 127.200000 | | White |   9.00 |   12 |   108.00 | 6.480000 | 114.480000 | | Rose  |   8.00 |    6 |    48.00 | 2.880000 |  50.880000 | +-------+--------+------+----------+----------+------------+ 3 rows in set (0.00 sec) mysql> mysql> # delete this sample table mysql> DROP TABLE wines; Query OK, 0 rows affected (0.00 sec)