Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BIGINT UNSIGNED VALUE IS out of range My SQL

Tags:

mysql

I'm getting the following error

#1690 - BIGINT UNSIGNED value is out of range in '(legends.spawns.quantity - tmp_field)'

Here is my query

SELECT drops.common, drops.uncommon, drops.rare, drops.legendary, spawns . *
     , ( quantity - COUNT( game_moblist.spawn_id ) ) AS quantity_to_spawn
     , mobs . * 
FROM spawns
     LEFT JOIN mobs
          USING ( mob_id ) 
     LEFT JOIN game_moblist
          USING ( spawn_id ) 
     LEFT JOIN drops ON ( 
               SELECT MAX( level ) 
                 FROM drops
                WHERE drops.type = mobs.drop_list
                  AND drops.level <= spawns.level ) 
GROUP BY spawn_id
HAVING quantity_to_spawn >=0
       AND next_spawn <=0

I've been staring at it a while the query is long I'm sorry.

spawns table - count game_moblist.spawn_id is 0 for all possible rows but 1 (I deleted a row to test the query)

The data otherwise is quite long and irrelevant to my question I think

Any idea how to get around this error?

like image 497
Shawn Avatar asked Jul 28 '12 05:07

Shawn


People also ask

What is unsigned BIGINT?

Description. A large integer. The signed range is -9223372036854775808 to 9223372036854775807 . The unsigned range is 0 to 18446744073709551615 . If a column has been set to ZEROFILL, all values will be prepended by zeros so that the BIGINT value contains a number of M digits.

Does MySQL support BIGINT?

MySQL supports the SQL standard integer types INTEGER (or INT ) and SMALLINT . As an extension to the standard, MySQL also supports the integer types TINYINT , MEDIUMINT , and BIGINT .

What does BIGINT mean in MySQL?

BIGINT is the MySQL data type that can be assigned to the columns of the table in which we want to store the whole numbers and we are aware that the range of the numbers that we will store in that column will be huge and not exceed the range of the BIGINT data type.


7 Answers

Please read "Out-of-Range and Overflow Handling".
It says:

As of MySQL 5.5.5, overflow during numeric expression evaluation results in an error. For example, the largest signed BIGINT value is 9223372036854775807, so the following expression produces an error.

mysql> SELECT 9223372036854775807 + 1;

ERROR 1690 (22003): BIGINT value is out of range in '(9223372036854775807 + 1)'

To enable the operation to succeed in this case, convert the value to unsigned;

mysql> SELECT CAST(9223372036854775807 AS UNSIGNED) + 1;
+-------------------------------------------+
| CAST(9223372036854775807 AS UNSIGNED) + 1 |
+-------------------------------------------+
|                       9223372036854775808 |
+-------------------------------------------+

A change to part of your query, as following, would solve the issue.

( CAST( quantity AS SIGNED ) - COUNT( game_moblist.spawn_id ) ) AS quantity_to_spawn

Otherwise you may require to change the sql_mode on unsigned operations.

mysql> SET sql_mode = 'NO_UNSIGNED_SUBTRACTION';

and then run your query to get desired output.

See also a similar posting answered on a forum here.

like image 112
Ravinder Reddy Avatar answered Oct 22 '22 19:10

Ravinder Reddy


To generalise the rule, MySQL will now refuse to substract an UNSIGNED operand from a SIGNED one.

Example : SELECT A - B; will fail if A is SIGNED whereas B is UNSIGNED.

Workarounds: Add 1.0 factor to the signed operand, so it implicitly casts it to FLOAT, or use CAST (B AS SIGNED), or even swap (B - A) and change the algorithm accordingly.

like image 30
Fabien Haddadi Avatar answered Oct 22 '22 18:10

Fabien Haddadi


I had the same problem, it occurred on a JOIN and couldn't figure out what was going on, in the end it was typo in the ON clause where I placed a minus sign instead of an equal sign. Might be stupid but I just didn't see it for about 30 minutes and maybe this could help someone!!!

like image 35
zwadder Avatar answered Oct 22 '22 17:10

zwadder


I don’t quite understand why everyone is saying unsigned. I have a special value in sql and also reported this error. I did it by converting this value to decimal.

cast(1000000000000000000 AS DECIMAL ( 35, 2 ))
like image 26
hu wentao Avatar answered Oct 22 '22 19:10

hu wentao


I actualy found that question why I was searching for solution. If you have same problem as I do, try disabling "unsigned" parameter.

It is quite possible that your code fails here:

(
quantity - COUNT( game_moblist.spawn_id )
)

because if result of that matematic operation is less than zero it will fail with "unsigned" parameter.

like image 33
Gonzi Avatar answered Oct 22 '22 19:10

Gonzi


Additional way is to use MySQL IF operator. This helped me when both columns were BIGINT and Unsigned.

like image 26
Andron Avatar answered Oct 22 '22 19:10

Andron


I had similar problem, This error also come if our column have 0 value and we try to update it with -1 value.

In my case MySQL query were Failing if column1 is already have value 0, i.e column1 = 0

For example:

UPDATE `table1` SET `column1` = `column1` - 1 WHERE `column2` = XYZ

This give error

BIGINT UNSIGNED value is out of range in ....

To counter this problem

UPDATE `table1`
SET `column1` = (
    CASE WHEN `column1` < 1
    THEN 0
    ELSE (`column1` - 1)
end)
WHERE `column2` = 1
LIMIT 1
like image 38
Asif Asghar Avatar answered Oct 22 '22 17:10

Asif Asghar