MySQL查询:将值减少1 [英] Mysql query: decrease value by 1

查看:104
本文介绍了MySQL查询:将值减少1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字段(整数或下拉列表)中包含的值减少1.我尝试了这3个查询,但没有一个能按预期工作:

I would like decrease by 1 the value contained inside a field (integer or drop-down). I tried these 3 queries but none of them work as expected:

UPDATE `my_table` SET `my_field` = 'my_field-1' WHERE `other` = '123'

UPDATE `my_table` SET `my_field` = 'my_field' -1 WHERE `other` = '123'

UPDATE `my_table` SET `my_field` = '-1' WHERE `other` = '123'

我在这里和Google上进行了搜索,但发现的所有解决方案都相似.知道为什么这对我不起作用吗?

I searched here and on Google but all solutions I found are similar. Any idea why this doesn't work at my side?

推荐答案

您不需要任何引号.

UPDATE my_table SET my_field = my_field - 1 WHERE `other` = '123'

要理解,这就像任何语言中的经典含义:我希望my_field等于my_field(当前值)减去1.
如果加上引号,则表示我希望my_field等于字符串:

To understand, it's like a classic affectation in any languages: "I want my_field being equal to my_field (the current value) minus 1.
If you put quotes, it means "I want my_field being equal to the string:

  1. 'my_field-1'(针对您的第一个查询)
  2. 'my_field' - 1(这至少对我来说什么都没有:字符串减去整数的结果是什么?)
  3. '-1',如果您的字段具有INTEGER签名类型,它将转换为-1.
  1. 'my_field-1' (for your first query)
  2. 'my_field' - 1 (which means nothing, at least for me: what the result of a string minus an integer?)
  3. '-1', which will be converted to -1 if your field has the INTEGER signed type.

在某些情况下(如果您的字段名称中包含空格或特殊字符),则可以使用反引号"将字段名称括起来:

In some cases (if you have spaces or special characters if your field name), you can surrounded the field name with `backticks`:

UPDATE my_table SET `my_field` = `my_field` - 1 WHERE  other = '123'

这篇关于MySQL查询:将值减少1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆