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

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

WBOY
WBOY轉載
2023-08-24 10:05:021365瀏覽

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刪除