MySQL CAST如何处理溢出?
MySQLCAST可以处理在数值表达式评估期间发生的溢出。假设数值表达式评估产生溢出,则MySQL会反映一条错误消息。现在要处理此溢出,我们可以借助CAST将数值更改为UNSIGNED。
例如,在将BIGINT最大值加1时,MySQL由于溢出而产生错误,如下所示-
mysql> Select 9223372036854775807 + 1; ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807+1)'
现在,借助CAST,MySQL可以按以下方式处理这种溢出:
mysql> Select CAST(9223372036854775807 AS UNSIGNED) +1; +------------------------------------------+ | CAST(9223372036854775807 AS UNSIGNED) +1 | +------------------------------------------+ | 9223372036854775808 | +------------------------------------------+ 1 row in set (0.07 sec)