S
S
Sergey Pugovkin2017-03-04 11:03:42
MySQL
Sergey Pugovkin, 2017-03-04 11:03:42

Subtracting columns results in a "Numeric value out of range" error, but isn't it that simple?

SELECT (`likes` - `dislikes`) AS `rating` FROM `article`

This query returns an error if `likes` and/or `dislikes` are unsigned:
SQLSTATE[22003]: Numeric value out of range: 1690 BIGINT UNSIGNED value is out of range

Why? `rating` is automatically cast to unsigned? Or what?
Let me clarify that all values ​​do not exceed the INTEGER range: −2 147 483 648 to 2 147 483 647

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Pugovkin, 2017-03-04
@Driver86

In short, as I thought :)
www.mysql.ru/docs/man/Column_types.html

Warning: be aware that when performing a subtraction between numeric values, one of which is of type UNSIGNED, the result will be unsigned!

M
mletov, 2017-03-04
@mletov

Look at the values ​​in the table for likes, dislikes and their difference. As well as valid values ​​for INT, BIGINT or what type they are assigned there.
https://dev.mysql.com/doc/refman/5.5/en/integer-ty...
Maybe your difference is less than -2147483648 (INT) | -9223372036854775808 (BIGINT)
or greater than 2147483647(INT) | 9223372036854775807(BIGINT)
stackoverflow.com/questions/11698613/bigint-unsign...

R
Rsa97, 2017-11-25
@rafamont

int arr[] = {1, 2, 3, 4, 5, 6};
int *arr1 = arr;
int *arr2 = arr+3;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question