Calculation : Select « Select Clause « SQL / MySQL






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)

   
    
    
    
    
    
    
  








Related examples in the same category

1.Define and use variable in select clause
2.Use defined variable in new select clause
3.Select clause with condition
4.Choose specific columns
5.Reference column in select command
6.Performing Mathematics
7.Simple Retrieval:Returning Multiple Columns
8.Simple Retrieval:Returning a Single Column
9.Simple Retrieval: Returning All Columns
10.Performing a Single Row INSERT
11.Retrieve all columns in a table
12.Using constant string value as comment message for aggregate function