在一个查询中更新/增加多个 MySQL 列

发布于 2024-09-12 09:48:31 字数 708 浏览 2 评论 0原文

我有这个查询,它有效...

UPDATE `contacts` 
       SET `calls_to`=`calls_to`+1 
  WHERE `contact_no` = '0412345678';

我还想做的是向成本字段添加一个值。根据我的理解,做到这一点的方法是......

UPDATE `contacts` 
       SET `calls_to` = `calls_to`+1, 
             `cost_to` = `cost_to`+0.25 
  WHERE `contact_no`='0412345678';

显然,正如我在这里发布的那样,它并没有像我预期的那样工作。

--更新--

根据要求,表结构..

id                  int(255) auto_increment
contact_owner  varchar(255)
contact_no       varchar(11)
contact_name   varchar(255)
calls_to            int(255)
txts_to             int(255)
time_talked_to   int(255)
cost_to            decimal(65,2)

I have this query, which works...

UPDATE `contacts` 
       SET `calls_to`=`calls_to`+1 
  WHERE `contact_no` = '0412345678';

What I also want to do is add a value to the cost field. From my understanding, the way to do this would be...

UPDATE `contacts` 
       SET `calls_to` = `calls_to`+1, 
             `cost_to` = `cost_to`+0.25 
  WHERE `contact_no`='0412345678';

Obviously, as I'm posting here, it's not working as I would expect.

--UPDATE--

As requested, the table structure..

id                  int(255) auto_increment
contact_owner  varchar(255)
contact_no       varchar(11)
contact_name   varchar(255)
calls_to            int(255)
txts_to             int(255)
time_talked_to   int(255)
cost_to            decimal(65,2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

失退 2024-09-19 09:48:31

检查 cost_to 的数据类型是否为 int。如果该列的值不为空,则更新该列。

UPDATE `contacts` 
       SET `calls_to` = `calls_to`+1, 
             `cost_to` = `cost_to`+0.25 
  WHERE `contact_no`='0412345678' AND
          calls_to is not null AND
          cost_to is not null;

Check if the datatype for cost_to is int or not.Also update the column if it's value is not null.

UPDATE `contacts` 
       SET `calls_to` = `calls_to`+1, 
             `cost_to` = `cost_to`+0.25 
  WHERE `contact_no`='0412345678' AND
          calls_to is not null AND
          cost_to is not null;
温柔嚣张 2024-09-19 09:48:31

乍一看,该查询看起来不错。 cost_to 字段的类型是什么?仔细检查它不是整数类型,因为这样您将无法得到您正在寻找的结果。 (作为测试,为 cost_to 添加一个更大的值,例如 4。)

On first glance the query looks fine. What is the type of the cost_to field? Double check it's not an integral type, since you will then not get the result you are looking for. (As a test, add a larger value, say, 4 to cost_to.)

┊风居住的梦幻卍 2024-09-19 09:48:31

我认为您的 calls_to 字段为 int ,cost_to 字段为不同类型,所以只有您没有得到结果。检查字段的类型。

I think your calls_to field as int and cost_to field as in different type, so only you didn't got the result. Check the type of the field.

童话里做英雄 2024-09-19 09:48:31

单表语法:

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]

多表语法:

UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]

对于单表语法,UPDATE 语句用新值更新指定表中现有行的列。 SET 子句指示要修改哪些列以及应为其指定的值。每个值都可以作为表达式或关键字 DEFAULT 给出,以将列显式设置为其默认值。 WHERE 子句(如果给定)指定标识要更新哪些行的条件。如果没有 WHERE 子句,则所有行都会更新。如果指定了 ORDER BY 子句,则按指定的顺序更新行。 LIMIT 子句对可以更新的行数设置限制。

对于多表语法,UPDATE 更新 table_references 中指定的每个表中满足条件的行。每个匹配行都会更新一次,即使它多次匹配条件。对于多表语法,不能使用ORDER BYLIMIT

Single-table syntax:

UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]

Multiple-table syntax:

UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]

For the single-table syntax, the UPDATE statement updates columns of existing rows in the named table with new values. The SET clause indicates which columns to modify and the values they should be given. Each value can be given as an expression, or the keyword DEFAULT to set a column explicitly to its default value. The WHERE clause, if given, specifies the conditions that identify which rows to update. With no WHERE clause, all rows are updated. If the ORDER BY clause is specified, the rows are updated in the order that is specified. The LIMIT clause places a limit on the number of rows that can be updated.

For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions. Each matching row is updated once, even if it matches the conditions multiple times. For multiple-table syntax, ORDER BY and LIMIT cannot be used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文