## Friday, April 22, 2016

### Math with MySQL

I thought I posted this long ago... oh well....

We all know that math is the fundamental aspect of all life and the common language used around the world if not beyond. MySQL, like all databases, can help you with numerous aspects of math.

Here is a list of the functions: https://dev.mysql.com/doc/refman/5.6/en/mathematical-functions.html

Here are some simple examples to get you started.
• The Quadratic Formula  ax^2 + bx + c = 0
``` # 2x^2  – 4x – 3 = 0. SET @a=  1; SET @b=  3; SET @c= -4; SET @XX = ( -(@b) - SQRT(  POW(@b,2)  -4 * @a * @c) / POW(@a,2) ) ; SET @YY = ( -(@b) + SQRT(  POW(@b,2)  -4 * @a * @c) / POW(@a,2) ) ;  SET @XXX = MOD(@YY, @XX); SELECT @XX / @XXX  as X; +------+ | X    | +------+ |   -4 | +------+ SELECT @YY / @XXX  as X ; +------+ | X    | +------+ |    1 | +------+ ```

• The Pythagorean Theorem (remember geometry 101):  A^2 + B^2 = C^2
``` SET @A = 14; SET @B = 48; SELECT @C := SQRT(POW(@A,2) +  POW(@B,2) ); +-------------------------------------+ | @C := SQRT(POW(@A,2) +  POW(@B,2) ) | +-------------------------------------+ |                                  50 | +-------------------------------------+ ```

So that solves C and of course you use this to solve for A as well.
``` SELECT @A := SQRT(POW(@C,2) - POW(@B,2)) ;  +-----------------------------------+ | @A := SQRT(POW(@C,2) - POW(@B,2)) | +-----------------------------------+ |                                14 | +-----------------------------------+ ```

• The logarithm and its identities   log xy = log x + log y
``` SET @X = 2; SET @Y = 3; SELECT concat(log(@X * @Y) ,' = ', log(@X) + log(@Y) ) as "logarithm and its identities"  ; +---------------------------------------+ | logarithm and its identities          | +---------------------------------------+ | 1.791759469228055 = 1.791759469228055 | +---------------------------------------+ ```
``` SET @V = 4;  # Vertices SET @E = 6;  # Edges SET @F = 4;  # Faces SELECT @V - @E + @F as Tetrahedron; SET @V = 8;  # Vertices SET @E = 12;  # Edges SET @F = 6;  # Faces SELECT @V - @E + @F as Hexahedron; SET @V = 12;  # Vertices SET @E = 30;  # Edges SET @F = 20;  # Faces SELECT @V - @E + @F as Icosahedron; SET @V = 12;  # Vertices SET @E = 30;  # Edges SET @F = 20;  # Faces SELECT @V - @E + @F as Icosahedron; ```
``` SET @lbs = 190; # lbs  SET @lb2gram = 453.6;  # 1 lbs = 453.6g  SET @lbstograms := @lbs * @lb2gram  / 1; SET @m := @lbstograms * 1 / 1000; SET @c := POW(3.00 * POW(10,8), 2 ); SELECT @E := @m  * @c ; +----------------+ | @E := @m  * @c | +----------------+ |     7.75656e18 | +----------------+  ```
``` SELECT SUM(.9/(9/10)); +----------------+ | SUM(.9/(9/10)) | +----------------+ |        1.00000 | +----------------+ ```