[英]Updating a mysql-table. one field = old_value - 1
What I need to do is: - Delete one entry from table formfields
- Update EVERY field with ordering > (ordering of the deleted field) - Set the ordering of the updated fields to the current value minus 1 (eg if current ordering on the field is 8, then it should be set to 7). 我需要做的是: - 从表格formfields
删除一个条目 - 使用排序更新每个字段>(删除字段的排序) - 将更新字段的顺序设置为当前值减1(例如,如果字段上的当前排序是8,那么它应该设置为7)。
Is that possible in one query? 这可能在一个查询中? Or how can I make it happen? 或者我该如何实现?
You can set up a transaction to do this all at once. 您可以设置一个事务来一次完成所有操作。
Given the following table: 鉴于下表:
CREATE TABLE `formfields` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`order_no` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
You can do something like the following: 您可以执行以下操作:
START TRANSACTION;
DELETE FROM formfields WHERE order_no = {YOUR_NUM};
UPDATE formfields SET order_no = order_no - 1 WHERE order_no > {YOUR_NUM};
COMMIT;
You obviously can't do the DELETE
and the UPDATE
in the same query, but you could use InnoDB, start a transaction, run the delete query and then use an update query such as... 您显然无法在同一查询中执行DELETE
和UPDATE
,但您可以使用InnoDB,启动事务,运行删除查询,然后使用更新查询,例如...
UPDATE formfields SET field_value = field_value - 1
WHERE field_value >= <VALUE YOU'VE JUST DELETED>
...prior to committing the transaction. ......在提交交易之前。
Doing the two queries back to back using a MyISAM table will work nine times out of ten (depending on load, etc.), but you really shouldn't go there. 使用MyISAM表背靠背做两个查询将在十分之一的情况下工作九次(取决于负载等),但你真的不应该去那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.