mysql更新increment int字段为空
我有一个非常大的表,其中有两个 INT 列,默认情况下这些列为空。这是一个问题,因为因为它们是 INT 字段,所以如果它们最初设置为 0,在很多情况下会有所帮助。
所以我的问题是,有没有办法可以更新和增量(+1)这些字段,而它们是这样的(默认为空)?顺便说一句..到目前为止我没有运气,似乎增量仅在默认值 = 0 时才起作用
..或者是我将默认值从 null 更改为无的唯一选择
I have a very large table with two INT columns that are null on Default. This is a problem because since they are INT fields, it would help in many cases if they were originally set to 0.
So my questions are, is there a way I can UPDATE and INCREMENT(+1) these fields while they are like this (null on Default)? BTW.. I didn't have luck so far, it seems increment only works when the default=0
..or is my only option to Change the Default to none from null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有关
IFNULL
的更多信息。如果第一个参数不为 NULL,则返回第二个参数,否则返回第二个参数。More info on
IFNULL
. It returns the first argument if it is notNULL
, the second otherwise.尝试将字段设置为
NOT NULL
来解决问题,以便使用默认值 0 而不是 null。另一个选项是每当列为空时将其设置为零。其他替代方法是发出 IFNULL如果该列为空,则返回 0,然后递增该列。
Try setting the field as
NOT NULL
to get away with the problem so that default value of 0 is used instead of null. The other option is to set column as zero whenever it is null.Other alternative would be to issue IFNULL to return 0 in case the column is null and then incrementing the column.
SQL 标准是使用
COALESCE()
;自 3.23 版(2001 年发布到生产环境)以来,MySQL 中已提供此功能:我看不出有任何理由在此处选择
IFNULL()
而不是COALESCE()
。希望这有帮助。
The SQL standard would be to use
COALESCE()
; this has been available in MySQL since version 3.23 (which was released into production in 2001):I can't see any reason to choose
IFNULL()
overCOALESCE()
here.Hope this helps.