BIGINT UNSIGNED value is out of range

前端 未结 7 969
不知归路
不知归路 2020-12-03 09:48

I am getting the error

BIGINT UNSIGNED value is out of range in \'(1301980250 - mydb.news_articles.date)\'

相关标签:
7条回答
  • 2020-12-03 10:05

    This can sometimes be caused by nulls in the data.

    Use IFNULL to set a default value (probably 0 for a timestamp is a poor default and actually in this case you might be better off excluding and null dates in the WHERE clause)

    SELECT (123456 - IFNULL(date, 0)) AS leVar

    0 讨论(0)
  • 2020-12-03 10:19

    maybe you can use cast

    SELECT *, ((1 / log(1301980250 - cast(date AS SIGNED))) * 175) as weight FROM news_articles ORDER BY weight;

    0 讨论(0)
  • 2020-12-03 10:19

    I just came across this issue doing an update on a field where the result ended up being less than 0.

    Solution: Verify that none of your updates cause your result to be less than 0 on an unsigned field.

    0 讨论(0)
  • 2020-12-03 10:20

    The problem was caused by unsigned integer overflow as suggested by wallyk. It can be solved by

    1. using SELECT *, ((1 / log((date - 1301980250) * -1)) * 175) as weight FROM news_articles ORDER BY weight; (This one worked for me) `
    2. Changing sql_mode parameter in my.cnf to NO_UNSIGNED_SUBTRACTION (haven't checked this)
    0 讨论(0)
  • 2020-12-03 10:23

    Nobody mentionned that the log() function is only defined for strictly positive arguments. Watch for this when using substractions inside of log().

    As for the original question, a key factor for resolution was to tell us the data type for the date column. If it is UNSIGNED, MySQL might not like it.

    The rule is that MySQL has a poor arithmetic algo, and can't figure out how to substract an operand B FROM another A (= do A-B) when A is coded on less bytes than B AND B > A.

    e.g. A = 12 and is SMALLINT, B = 13 AS INT, then MySQL can't figure out what A-B is (-1 !)

    To make MySQL content, just expand the coding length of operand A. How? Using CAST(), or multiplying A by a decimal number.

    As one can see, it is less a problem of overflow than a problem of handling the sign in the arithmetics of MySQL. A microprocessor, or better, a human, has no problems to perform this kind of arithmetics...

    Using CAST() is the way, or for short, just provoke the implicit cast by multiplying operand A by 1. (or 1.0):

    e.g

    1.*A - B
    
    0 讨论(0)
  • 2020-12-03 10:24

    Any date value after 2011-04-04 22:10:50 PDT (2011-04-05 05:10:50 utc) will cause this error since that would make the expression negative.

    0 讨论(0)
提交回复
热议问题