如何排序MySql数据库? [英] How to sort the MySql Database?
问题描述
我已经在MySql数据库"orkut"中存储了各种记录.现在,我想通过一个Java程序对该数据库进行排序.我已经通过jdbc驱动程序连接到数据库. 现在,我想按类型为"int"的字段"number"的降序对数据库进行排序,但不知道这些命令.我有"con"参考变量,它表示与MySql数据库的连接.
I have stored various records in the MySql database "orkut". Now I want to sort that database through a java program. I have connected to the database through the jdbc driver. Now I want to sort that database in decreasing order of the field "number" of type "int" but don't know the commands. I have "con" reference variable which denotes the connection to the MySql database.
还有一件事情,字段"sr_no"表示序列号.记录,它不是主键.
One more thing, there is a field "sr_no" that denotes the serial no. of the record and it is not the primary key.
我希望在将数据库排序为序列号后,该字段不会更改.在更改记录顺序时不应更改.
I want that this field won't change after sorting the database as the serial no. should not change on changing the order of the records.
我希望这种排序永久存储在同一数据库中.我不想对ResultSet进行排序. 我想要排序的数据库.
I want this sorting permanently stored on the same database. I don't want sorted ResultSet. I want sorted database.
推荐答案
不要尝试通过Java对其进行排序,否则您会自杀. SQL有一个order by
子句可以做到这一点.这是SQL:
Don't try to sort this through Java--you'll kill yourself trying. SQL has an order by
clause that does exactly this. Here's the SQL:
select
number,
sr_no
from
tbl
order by
number desc
还请注意,您不能具有永久排序的数据库.数据的存储方式不会使其以您选择的任何顺序存储.除非您在查询中使用order by
,否则您应该永远不要依赖数据库的顺序.
Also note that you cannot have a permanently sorted database. The way that the data is stored does not lend itself to being stored in whatever order you choose. You should never count on the order of a database to be the same, unless you use an order by
in your query.
这篇关于如何排序MySql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!