有兄弟问我SQL语法,N久没搞这东西,翻了一下以前的资料,呵呵,还真的找到一些灵感,想想以前编程的日子真是一种快乐,现在是什么?也许是痛并快乐者吧.我爱生活:
第一种方法:
select a.name,a.total,(select count(*) from tablename b where b.name=a.nameand b.total>a.total) as mcfrom tablename aorder by a.total
第二种方法:
SELECT ID,IDENTITY(int, 1,1) as 名次into #tmpfrom 表 order by 总分update 表 set 名次=b.名次from 表 a,#tmp bwhere a.id=b.idDROP TABLE #tmp
第三种方法:
with aqr do begin //建立临时表 SQL.Clear; SQL.Add('create table tmp (id smallint,总分 smallint,名次 counter);'); ExecSQL; //排名 SQL.Clear; SQL.Add('insert into tmp (id,总分) SELECT ID,总分 from 成绩表 order by 总分 desc;'); ExecSQL; //更改表结构 SQL.Clear; SQL.Add('ALTER TABLE tmp ALTER COLUMN 名次 integer;'); ExecSQL; //解决重复排名 SQL.Clear; SQL.Add('insert into tmp (id,总分,名次) select 0 as id,总分,mc as 名次 from (select 总分,min(名次) as mmc from tmp group by 总分);'); ExecSQL; //更新 SQL.Clear; SQL.Add('update 成绩表,tmp set 成绩表.名次=tmp.名次 where 成绩表.总分=tmp.总分 and tmp.id=0;'); ExecSQL; //删除临时表 SQL.Clear; SQL.Add('DROP TABLE tmp;'); ExecSQL; end;
星期二, 三月 13, 2007
订阅:
博文评论 (Atom)

没有评论:
发表评论