如何計算 MySQL 中的一個數字的冪?


如需計算一個數的冪,請使用 POWER() 函式。我們首先建立一個表格 −

mysql> create table DemoTable
   (
      Amount int
   );
Query OK, 0 rows affected (0.89 sec)

使用 insert 命令在表中插入一些記錄 −

mysql> insert into DemoTable values(64);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values(144);
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values(400);
Query OK, 1 row affected (0.13 sec)

使用 select 語句顯示錶格中的所有記錄 −

mysql> select *from DemoTable;

這將產生以下輸出 −

+--------+
| Amount |
+--------+
| 64     |
| 144    |
| 400    |
+--------+
3 rows in set (0.00 sec)

以下是計算一個數的冪的查詢 −

mysql− select Amount,power(Amount,2) AS CURRENT_AMOUNT from DemoTable;

這將產生以下輸出 −

+--------+----------------+
| Amount | CURRENT_AMOUNT |
+--------+----------------+
| 64     | 4096           |
| 144    | 20736          |
| 400    | 160000         |
+--------+----------------+
3 rows in set (0.04 sec)

更新於: 22-8-2019

93 次瀏覽

開啟您的 職業 生涯

透過完成課程獲得認證

入門
廣告
© . All rights reserved.