一个表里删除一行之后如何对rowNum列进行重排序

aj3423 2009-05-20 10:12:59
mysql, 一个表里有 10条记录,有一个rowNum列,记录的是行号,
0
1
。。。
9


如果把第3,5,7条记录删掉, 我希望删完之后能这样
0
1
2
3
4
5
6

而不是
0
1
3
5
7
8
9

请问咋整。
...全文
65 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
WWWWA 2009-05-20
  • 打赏
  • 举报
回复
update t1 a1 inner join
(select a.id,count(b.id) as id1 from ttq a
left join ttq b on a.id>b.id
group by a.id) b1
set a1.id=b1.id1
where a1.id=b1.id
WWWWA 2009-05-20
  • 打赏
  • 举报
回复
set @ee=-1;
select id,@ee:=@ee+1 from ttq

or

select a.id,count(b.id) from ttq a
left join ttq b on a.id>b.id
group by a.id
ACMAIN_CHM 2009-05-20
  • 打赏
  • 举报
回复

如果一这要更新这个字段可以。

mysql> select * from tx;
+----+
| id |
+----+
| 0 |
| 1 |
| 2 |
| 4 |
| 6 |
| 8 |
| 9 |
+----+
7 rows in set (0.00 sec)

mysql> update tx t1,
-> (select id,(select count(*) from tx where id<t.id) as newid from tx t) t2
-> set t1.id=t2.newid
-> where t1.id=t2.id;
Query OK, 4 rows affected (0.06 sec)
Rows matched: 7 Changed: 4 Warnings: 0

mysql> select * from tx;
+----+
| id |
+----+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
+----+
7 rows in set (0.00 sec)

mysql>
WWWWA 2009-05-20
  • 打赏
  • 举报
回复
如果是:
1、 可以用JOIN实现

2、用变量累加得到行号
ACMAIN_CHM 2009-05-20
  • 打赏
  • 举报
回复

如果是这样,其实不建议保留这个行号。有些冗余了。

你可以根据下面贴子中的方法动态生成行号。

http://blog.csdn.net/ACMAIN_CHM/archive/2009/04/20/4095531.aspx
MySQL中的ROWNUM的实现

WWWWA 2009-05-20
  • 打赏
  • 举报
回复
rowNum是否是主键?

57,062

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧