小弟刚处理完这类问题,和各们一样自已写一个函数来解决。
具体问题是:有一十选手的分数,和编号等信息存入数据表(zf)中,要求将各名选手的成绩名次排 出来,也存入表中,便于经后输出。
1.处理之前先定义一个结构体:str_mysort{
integer sort
integer id
real score
}
2.定义排名函数,在程序中调用它:
f_sort()
{
nt i,j
str_mysort a[10],t
//从数据库中提取总分,并给结构体赋初值
for i=1 to 10
select zf
into :a[i].score
from zf
where id=:i;
a[i].sort=0
a[i].id=i
next
//用冒泡法给数组a排序
for i=1 to 10
for j=2 to (11 - i)
if (a[j - 1].score<a[j].score) then
t.id=a[j - 1].id
t.score=a[j - 1].score
a[j - 1].id=a[j].id
a[j - 1].score=a[j].score
a[j].id=t.id
a[j].score=t.score
end if
next
next
//设置数组序号分量的值
a[1].sort=1
for i=2 to 10
if(a[i].score = a[i - 1].score) then
a[i].sort=a[i - 1].sort
else
a[i].sort=a[i - 1].sort + 1
end if
next
//更新数据表中名次的值
for i=1 to 10
update zf
set mc =:a[i].sort
where id =:a[i].id;
next
}