在一个查询中更新/增加多个 MySQL 列
我有这个查询,它有效...
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查
cost_to
的数据类型是否为 int。如果该列的值不为空,则更新该列。Check if the datatype for
cost_to
is int or not.Also update the column if it's value is not null.乍一看,该查询看起来不错。
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.)我认为您的 calls_to 字段为 int ,cost_to 字段为不同类型,所以只有您没有得到结果。检查字段的类型。
I think your
calls_to
field as int andcost_to
field as in different type, so only you didn't got the result. Check the type of the field.单表语法:
多表语法:
对于单表语法,UPDATE 语句用新值更新指定表中现有行的列。 SET 子句指示要修改哪些列以及应为其指定的值。每个值都可以作为表达式或关键字 DEFAULT 给出,以将列显式设置为其默认值。 WHERE 子句(如果给定)指定标识要更新哪些行的条件。如果没有 WHERE 子句,则所有行都会更新。如果指定了 ORDER BY 子句,则按指定的顺序更新行。 LIMIT 子句对可以更新的行数设置限制。
对于多表语法,UPDATE 更新 table_references 中指定的每个表中满足条件的行。每个匹配行都会更新一次,即使它多次匹配条件。对于多表语法,不能使用ORDER BY和LIMIT。
Single-table syntax:
Multiple-table syntax:
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.