English 中文(简体)
删除 Mysql 然后更新
原标题:mysql delete then update
  • 时间:2012-05-24 03:53:13
  •  标签:
  • php
  • mysql

MySQL 中是否有办法/方法, 如果我掉下一行, 下一行可能会更新? 为了说明我的意思, 比如说, 我这里有一个表格 :

+--------------+
|  id  | order |
+--------------+
|  115 |     1 |
|  116 |     2 |
|  117 |     3 |
|  118 |     1 |
|  119 |     2 |
|  120 |     6 |
|  121 |     7 |
|  122 |     8 |
+--------------+

如果行动成功,我就要投下 id = 117 ,我希望 id = 118 从1号到3号的顺序,这是目前投下 id = 117 的顺序。然后使 id = 119 成为4号,然后 id = 120 to 5号,等等。

我这样做是因为我的PHP计划里 有一些订购行。

最佳回答

如果您需要将顺序列完全重新填入,因为如果删除行号 ID 118 或 119,您需要更改 118,119,120 等,直到表格结束

 update order1,(select @rownum:=0)dummy set order1 = 
(@rownum =@rownum+1);

因此,如果删除118项,则在此情况下删除118项。

Delete from YourTable where id = 118

运行上述更新脚本, 有效

问题回答

您需要 MySQL 5+ 。 从 MySQL 5 开始, 我们有触发器 。 触发器是当特定表格上出现插入、 更新或删除动作时点火或执行的程序 。 因此您可以创建一个“ BEforE ” “ DELETE ” 触发器, 您可以在同一个表格上打开一个 cursor , 它将在 < code> OLD.id 之后选择带有ID的行, 在那里, < code> OLD 意味着删除的行数据表格( 所谓的魔术表格) 。 那么, 只需发布所需的 UPDATE 命令即可更新其中的相关行 。

您可以将删除的行数据称为 OLD.col_name 。 在 INSERT UPDATE 触发器中, 您也可以访问 NEW.col_ name 来引用插入的数据。 只有 UPDATE 触发器中, 您才能访问 NEW OLD 魔术表格。

Links: MySQL triggers Cursors





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签