首頁 > 資料庫 > mysql教程 > MySQL 如何處理數值表達式評估期間的溢位?

MySQL 如何處理數值表達式評估期間的溢位?

WBOY
發布: 2023-08-24 10:05:02
轉載
1361 人瀏覽過

MySQL 如何处理数值表达式评估期间的溢出?

As we know that MySQL will produce an error if overflow occurs during the assessment of numeric expressions. For example, the largest signed BIGNT is 9223372036857775807233720368577758072337203685 775

mysql> Select 9223372036854775807 + 1;
ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807+1)'
登入後複製

MySQL can handle such kind of overflows in following ways:

BY CONVERTING VALUE TO UNSIGNED

#MySQL enables such kind of operations by converting the values to signed as foluns −

mysql> Select CAST(9223372036854775807 AS UNSIGNED) +1;
+------------------------------------------+
| CAST(9223372036854775807 AS UNSIGNED) +1 |
+------------------------------------------+
|                      9223372036854775808 |
+------------------------------------------+
1 row in set (0.07 sec)
登入後複製

透過使用精確值算術

MySQL可以使用精確值算術來處理上述表達式。這是因為溢出發生取決於操作數的範圍。例如,可以透過使用DECIMAL值來執行上述計算,如下所示−

mysql> Select 9223372036854775807.0 + 1;
+---------------------------+
| 9223372036854775807.0 + 1 |
+---------------------------+
|     9223372036854775808.0 |
+---------------------------+
1 row in set (0.01 sec)
登入後複製

以上是MySQL 如何處理數值表達式評估期間的溢位?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板