码迷,mamicode.com
首页 > 数据库 > 详细

mysql|unsigned 与 signed 类型

时间:2020-05-03 18:38:25      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:使用   包含   取值   interval   max   weight   HERE   over   mic   

通过mysql的数值类型设置,控制数值的正负

1,如何使用

在mysql的编辑器中,可以直接定义 bigint(20) unsigned

技术图片

 

 

 2, 发挥的作用

技术图片

一般默认定义的数据类型为signed(有符号类型),取值返回包含有负数范围,一般正负值的差依然等于无符号类型的范围的上限值。

当定义为unsigned(无符号类型)时, 取值范围仅为正数范围,下限值为0;

3,当设置为unsigned时候,报错BIGINT UNSIGNED value is out of range....如何解决

使用unsigned限制数值范围为正数的时候,如果执行相减操作产生负数;就会报错;解决方法

-- 需要添加四个计算值计算每日变化数据
-- dayConfirmed: 每日确诊
-- dayRecovered: 每日康复
-- dayDeaths: 每日死亡
-- dayExisting: 当日现存
select n.id,
       n.region_id,
       n.region_parent_id,
       n.recovered,
       n.deaths,
       n.day_date,
       n.confirmed,
       (cast(n.confirmed as signed) - cast(m.confirmed as signed)) as dayConfirmed,
       (cast(n.recovered as signed) - cast(m.recovered as signed)) as dayRecovered,
       (cast(n.deaths as signed) - cast(m.deaths as signed))       as dayDeaths,
       (cast(n.confirmed  as signed) - cast(n.recovered  as signed) - cast(n.deaths  as signed))    as dayExisting
from region_data_total as n
         join region_data_total as m on date_sub(n.day_date, interval 1 day) = m.day_date
where n.region_id = m.region_id and n.day_date = (select max(day_date) from region_data_total);

核心: 使用 cast(targetCol as signed) 将所有涉及到的unsigned字段先转化为signed类型后,再进行运算

相关内容补充:

MYSQL的数据类型(菜鸟教程):

  • https://www.runoob.com/mysql/mysql-data-types.html 

mysql|unsigned 与 signed 类型

标签:使用   包含   取值   interval   max   weight   HERE   over   mic   

原文地址:https://www.cnblogs.com/bennyjane/p/12822744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!