Showing posts with label arithmetic. Show all posts
Showing posts with label arithmetic. Show all posts

Tuesday, 20 December 2011

Arithmetic in MySQL

Here are some examples of arithmetic in MySQL. They are fairly self explanatory but there are a few points to note:
  1. You can select pi directly.
  2. The square root of -2 gives the answer -NaN. NaN stands for Not a Number.
  3. The order of precedence seems to be fairly standard and you can alter it by using brackets.
mysql> select 5+3/2, (5+3)/2;
+--------+---------+
| 5+3/2  | (5+3)/2 |
+--------+---------+
| 6.5000 |  4.0000 |
+--------+---------+
1 row in set (0.00 sec)
 
mysql> select pi();
+----------+
| pi()     |
+----------+
| 3.141593 |
+----------+
1 row in set (0.00 sec)
 
mysql> select power(2,3), power(4,0.5);
+------------+--------------+
| power(2,3) | power(4,0.5) |
+------------+--------------+
|          8 |            2 |
+------------+--------------+
1 row in set (0.00 sec)
 
mysql> select power(-2,0.5);
+---------------+
| power(-2,0.5) |
+---------------+
|          -NaN |
+---------------+
1 row in set (0.00 sec)
 
mysql> select power(-3,2);
+-------------+
| power(-3,2) |
+-------------+
|           9 |
+-------------+
1 row in set (0.00 sec)
 
mysql>