如果启用了SQL严格模式,则默认情况下,整数值之间的减法(其中一个为UNSIGNED类型)将产生无符号结果。但是,如果结果为负数,则MySQL会产生错误。可以通过以下示例进行观察-
mysql> SET sql_mode = ''; mysql> Select CAST(0 AS UNSIGNED) -1; ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in '(cast(0 as unsigned) - 1)'
上面查询后的错误表明,数值算术表达式后,它是一个溢出。
现在,可以在启用NO_UNSIGNED_SUBTRACTION SQL模式的帮助下进行处理。启用此模式后,结果将为-1而不是错误。
mysql> Set sql_mode = 'NO_UNSIGNED_SUBTRACTION'; mysql> Select CAST(0 AS UNSIGNED) -1; +------------------------+ | CAST(0 AS UNSIGNED) -1 | +------------------------+ | -1 | +------------------------+ 1 row in set (0.00 sec)