且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用DESC列来更新SQL中的表?

更新时间:2023-02-05 08:41:46

使用实际的表格设计,当你改变时记录的得分,你必须通过所有表重新计算排名。



您可以考虑删除您的Rank列,并计算查询中的排名代替。类似于:

With your actual table design, when you change the score of a record, you have to recompute the ranks through all the table.

You may consider getting rid of your Rank column, and compute the rank in the query instead. Something like:
SELECT
   t.Id
 , t.Score
 , ROW_NUMBER() OVER (ORDER BY t.Score DESC) AS Rank
 , t.Name
 , t.Photo
FROM TableName AS t



希望这会有所帮助。



更正了OVER子句。 [/ edit]


Hope this helps.

[edit] Corrected OVER clause. [/edit]


你没有在SQL中订购表 - 当你从数据库中提取它们时,你可以在它们填充表和视图的SELECT查询中对它们进行排序。如果你没有指定一个订单,那么SQL可以***地按照它认为方便的任何顺序服务器行 - 如果你连续两次发出相同的查询,它可能不一样。



因此,请查看绑定GridView时使用的SELECT查询,并确保其形式为:

You don't "order tables" in SQL - you order them when you pull them from the DB as part of the SELECT query that you populate your tables and views with. If you do not specify an order then, then SQL is at liberty to server rows in any order it finds convenient - and that may not be the same if you issue the same query twice in a row.

So look for the SELECT query that you use when you bind the GridView, and make sure it is of the form:
SELECT * FROM MyTable ORDER BY MyRankingColumn DESC