当前位置: 首页 > 编程笔记 >

删除一行并在MySQL中使用正确的ID对其他行重新排序?

韦澄邈
2023-03-14
本文向大家介绍删除一行并在MySQL中使用正确的ID对其他行重新排序?,包括了删除一行并在MySQL中使用正确的ID对其他行重新排序?的使用技巧和注意事项,需要的朋友参考一下

为了理解这个概念,让我们首先创建一个表。创建表的查询如下

mysql> create table ReorderSortDemo
   -> (
   -> UserId int
   -> );

使用insert命令在表中插入一些记录。查询如下-

mysql> insert into ReorderSortDemo values(14);
mysql> insert into ReorderSortDemo values(4);
mysql> insert into ReorderSortDemo values(6);
mysql> insert into ReorderSortDemo values(3);
mysql> insert into ReorderSortDemo values(8);
mysql> insert into ReorderSortDemo values(18);
mysql> insert into ReorderSortDemo values(1);
mysql> insert into ReorderSortDemo values(11);
mysql> insert into ReorderSortDemo values(16);

使用select语句显示表中的所有记录。查询如下-

mysql> select *from ReorderSortDemo;

以下是输出

+--------+
| UserId |
+--------+
|     14 |
|      4 |
|      6 |
|      3 |
|      8 |
|     18 |
|      1 |
|     11 |
|     16 |
+--------+
9 rows in set (0.00 sec)

首先从表中删除一行,然后使用update命令对其他行重新排序。查询如下-

mysql> delete from ReorderSortDemo where UserId=8;

删除后,让我们再次检查表记录。查询如下-

mysql> select *from ReorderSortDemo;

输出如下

+--------+
| UserId |
+--------+
|     14 |
|      4 |
|      6 |
|      3 |
|     18 |
|      1 |
|     11 |
|     16 |
+--------+
8 rows in set (0.00 sec)

这是重新排序其他列的查询

mysql> update ReorderSortDemo
   -> set UserId=UserId-1
   -> where UserId > 8;
Rows matched: 4 Changed: 4 Warnings: 0

让我们再次检查表记录。查询如下-

mysql> select *from ReorderSortDemo;

输出如下

+--------+
| UserId |
+--------+
|     13 |
|      4 |
|      6 |
|      3 |
|     17 |
|      1 |
|     10 |
|     15 |
+--------+
8 rows in set (0.00 sec)
 类似资料:
  • 我正在尝试使用query-更新行(删除列str_column中的连字符和连字符后的所有字符)- 但上面的查询是删除匹配的行而不是更新。 我不明白,如何更新查询可以删除行??

  • 问题内容: 我有一个包含以下字段的表: 现在,我需要删除具有same的行。一种方法是使用以下SQL以及脚本( ): 运行此查询后,我可以使用服务器端脚本删除重复项。 但是,我想知道是否只能使用SQL查询来完成。 问题答案: 一种简单的方法是在3列上添加索引。在编写语句时,请包含关键字。像这样: 这将删除所有重复的行。作为一项额外的好处,重复的将来会出错。和往常一样,您可能需要在运行类似内容之前进行

  • 我有: 三个表:、和. CursorLoader和CursorAdapter:和. ListView:一个简单的ListView. 是否可以坚持使用并使用另一个表中的列实现对的排序?

  • 基于Spark Scala代码,我可以看到在内部调用sort方法,该方法执行全局排序。 https://github.com/apache/spark/blob/branch-2.4/sql/core/src/main/scala/org/apache/spark/sql/dataset.scala 而且drop_duplicates(cols)方法也按照bellow spark代码转换为聚合(f

  • 这是一个古老的问题(#1、#2),我仍然在纠结于这个问题。现在尝试使用一个简单的SQL查询: 正在努力进行此SQL查询: 我需要所有的post_id,其中='services'和='publish',并且关联的()的值为1(一)。然后使用我使用另一个()传递的另一个元值对结果进行排序。如果我从查询中删除,那么就会得到这些post ID,但对于上面的查询,我什么也得不到。 我该怎么做呢?

  • 我需要删除表中的所有行,但当我添加新行时,我希望主键ID(具有自动增量)分别从1开始从0开始。