维护数据库表行的排序顺序 [英] Maintaining sort order of database table rows

查看:227
本文介绍了维护数据库表行的排序顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我在数据库表中包含有关每一行的新闻文章的信息。该表有一个整数排序列,用于指示文章在网站上的呈现顺序。我如何最好地实施和维护这种排序顺序。



我想避免的问题是将文章编号为1,2,3,4,...,100而当第50号文章突然变得有趣时,它的排序号设置为1,然后它们之间的所有文章必须将其排序号增加一个。



确定初始设置排序数字为100,200,300,400等,留下一些空间来移动,但在某些时候它会破裂。



有没有正确的方法来做到这一点,也许是一个完全不同的方法?






已添加1



所有文章标题显示在链接到内容的列表中,所以是所有排序的项目一次显示。



添加2



项目不一定移动到列表的顶部;任何项目都可以放在有序列表中的任何位置。

解决方案

如果我正确理解您的问题;我会使用一列包含某些类型的排名索引号。该号码不一定是唯一的。您将需要提出一些类型的算法来计算排名。



然后在排名列中排序,使用辅助列来处理排名关系(也许创建日期或东西)。


Say I have at database table containing information about a news article in each row. The table has an integer "sort" column to dictate the order in which the articles are to be presented on a web site. How do I best implement and maintain this sort order.

The problem I want to avoid is having the the articles numbered 1,2,3,4,..,100 and when article number 50 suddenly becomes interesting it gets its sort number set to 1 and then all articles between them must have their sort number increased by one.

Sure, setting initial sort numbers to 100,200,300,400 etc. leaves some space for moving around but at some point it will break.

Is there a correct way to do this, maybe a completely different approach?


Added-1:

All article titles are shown in a list linking to the contents, so yes all sorted items are show at once.

Added-2:

An item is not necessarily moved to the top of the list; any item can be placed anywhere in the ordered list.

解决方案

If I understand your problem correctly; I would use a column containing some type of ranking index number. This number doesn't have to be unique. You will need to come up with some type of algorithm to calculate the rank.

Then just sort on the ranking column, with a secondary column to handle ranking ties (maybe the create date or something).

这篇关于维护数据库表行的排序顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆