如何使用SQL(MySQL)对表中的列进行排序?
我使用Hibernate和索引列映射了一个列表。该表如下所示:如何使用SQL(MySQL)对表中的列进行排序?,mysql,sql,hibernate,Mysql,Sql,Hibernate,我使用Hibernate和索引列映射了一个列表。该表如下所示: mysql> select * from chart; +----+--------+------+-----------------------+ | id | report | indx | name | +----+--------+------+-----------------------+ | 2 | 1 | 0 | Volume |
mysql> select * from chart;
+----+--------+------+-----------------------+
| id | report | indx | name |
+----+--------+------+-----------------------+
| 2 | 1 | 0 | Volume |
| 3 | 2 | 0 | South Africa (Volume) |
| 5 | 2 | 2 | People |
| 6 | 2 | 3 | Platforms |
| 7 | 2 | 4 | People (Gender) |
+----+--------+------+-----------------------+
如您所见,indx=1的report=2的图表id=4已被删除
我需要消除间隙,以便给定报告的所有indx值从0开始按顺序运行。我可以编写Java代码来解决这个问题,但SQL解决方案将更易于部署
有人知道怎么做吗?我正在使用MySQL。如果这项技术在您的MySQL版本上有效,您可以试试
SET @row := -1;
UPDATE chart
SET indx = @row := @row + 1
WHERE report = 2
ORDER BY indx
但我认为最好改变设计。你真的不应该依赖于应用程序中的无间隙id!