tags:

views:

91

answers:

2

I'm run this query:

SELECT id,like - dislike as result
FROM mytable

Where the column like and dislike are unsigned integer. If the column dislike is greater than like mysql return number like 18446744073709551596, so seem that mysql treat this like unsigned and can't return negative number but continue the computation from a sort of MAX_UNSIGNED_INT. How can I have the correct result

+1  A: 

Try casting the two values (or maybe only on of them)

SELECT id, convert(like, SIGNED ) - convert(dislike, SIGNED ) as result
FROM mytable

or only the result

SELECT id, convert(like - dislike, SIGNED ) as result
FROM mytable

In the first way you can get type overflow! The Second way is better, but I'm not sure it works with mysql.

anthares
i've just try something like CAST(like as UNSIGNED) - CAST(like as UNSIGNED) but doesn't work
Luca Bernardi
cast as signed :). With CAST(like as UNSIGNED) you basically cast from UNSIGNED to UNSIGNED which is the same
anthares
damn! you're right...sorry but today is a very long day.
Luca Bernardi
Hahah :) True story!
anthares
A: 

You could try casting them as Int:

SELECT id, CAST(like AS INT) - CAST(dislike AS INT) as result FROM mytable
Bobby