1. 程式人生 > >BIGINT UNSIGNED value is out of range in..的解決方法

BIGINT UNSIGNED value is out of range in..的解決方法

今天在做一個功能的時候,用到兩個時間戳相減來做查詢條件,由於其兩個欄位都是unsigned的,並兩個的大小是不一樣。所以直接相減查詢的時候,

就出現ERROR 1690 (22003): BIGINT UNSIGNED value is out of range in..的錯誤,因為結果可能會出現負數。。。。

過後查資料,發現可以通過下面的方法來解決:

  1. mysql> select lastvisit, lastactivity from pre_common_member_status limit 1;  
  2. +------------+--------------+
  3. | lastvisit  | lastactivity |  
  4. +------------+--------------+
  5. | 1199200260 |   1198336989 |  
  6. +------------+--------------+
  7. 1 row inset (0.01 sec)  
  8. mysql> select lastvisit-lastactivity from pre_common_member_status limit 1;  
  9. +------------------------+
  10. | lastvisit-lastactivity |  
  11. +------------------------+
  12. |                 863271 |  
  13. +------------------------+
  14. 1 row inset (0.05 sec)  
  15. mysql> selectabs(lastvisit-lastactivity) from pre_common_member_status limit 1;  
  16. +-----------------------------+
  17. abs(lastvisit-lastactivity) |  
  18. +-----------------------------+
  19. |                      863271 |  
  20. +-----------------------------+
  21. 1 row in
    set (0.03 sec)  
  22. mysql> select lastactivity-lastvisit from pre_common_member_status limit 1;  
  23. ERROR 1690 (22003): BIGINT UNSIGNED value isoutof range in '(`discuz`.`pre_c  
  24. ommon_member_status`.`lastactivity` - `discuz`.`pre_common_member_status`.`las  
  25. tvisit`)'  
  26. mysql> selectcast(lastactivity as signed)-cast(lastvisit as signed) from pre_co  
  27. mmon_member_status limit 1;  
  28. +--------------------------------------------------------+
  29. cast(lastactivity as signed)-cast(lastvisit as signed) |  
  30. +--------------------------------------------------------+
  31. |                                                -863271 |  
  32. +--------------------------------------------------------+
  33. 1 row inset (0.02 sec)  
  34. mysql> selectabs(cast(lastactivity as signed)-cast(lastvisit as signed)) from p  
  35. re_common_member_status limit 1;  
  36. +-------------------------------------------------------------+
  37. abs(cast(lastactivity as signed)-cast(lastvisit as signed)) |  
  38. +-------------------------------------------------------------+
  39. |                                                      863271 |  
  40. +-------------------------------------------------------------+
  41. 1 row inset (0.00 sec)  
  42. mysql>