Friday, April 22, 2016

數學與MySQL

The Original post: http://anothermysqldba.blogspot.com/2016/04/math-with-mysql.html

我以為我張貼了這個很久以前...哦....

我們都知道,數學是所有生命的基本方面,如果沒有超越世界各地使用的通用語言。 MySQL的,像所有的數據庫,可以幫助您與數學的許多方面。

下面是功能列表: https://dev.mysql.com/doc/refman/5.6/en/mathematical-functions.html

下面是一些簡單的例子,讓你開始。
  • 二次公式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 |
+------+

  • 勾股定理(記住幾何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 |
+-------------------------------------+


這樣解決了C和你當然使用這個求解的為好。

SELECT @A := SQRT(POW(@C,2) - POW(@B,2)) ;
+-----------------------------------+
| @A := SQRT(POW(@C,2) - POW(@B,2)) |
+-----------------------------------+
| 14 |
+-----------------------------------+


  • 對數和它的身份登錄XY =日誌X +記錄Y
http://www.businessinsider.com/the-17-equations-that-changed-the-world-2012-7#the-logarithm-and-its-identities-2


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 |
+---------------------------------------+

  • 歐拉公式為多面體:F - E + V = 2
http://www.businessinsider.com/the-17-equations-that-changed-the-world-2012-7#eulers-formula-for-polyhedra-6

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;

  • 相對論電子商務愛因斯坦的相對論= mc ^ 2

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 |
+----------------+

  • 1 = 0.9999 .....

SELECT SUM(.9/(9/10));
+----------------+
| SUM(.9/(9/10)) |
+----------------+
| 1.00000 |
+----------------+