在MySql中,假设有这样两列:
stuID stuScore
01 90
02 95
03 91
04 70
05 82
然后我想让MySql自动根据成绩生成名次,生成第三列stuRank该怎么办?
或者说我建表的时候直接建三列。
不能是在数据库端写个sql语句简单地执行,因为我在程序中还要用这个表,会往里面插入数据。
stuID stuScore
01 90
02 95
03 91
04 70
05 82
然后我想让MySql自动根据成绩生成名次,生成第三列stuRank该怎么办?
或者说我建表的时候直接建三列。
不能是在数据库端写个sql语句简单地执行,因为我在程序中还要用这个表,会往里面插入数据。
if object_id('tempdb.dbo.#') is not null drop table #
create table #(stuID varchar(8), stuScore int)
insert into #
select '01', 90 union all
select '02', 95 union all
select '03', 91 union all
select '04', 70 union all
select '05', 82select *, stuRank = (select count(1) from # where stuScore >= t.stuScore) from # as t/*
stuID stuScore stuRank
-------- ----------- -----------
01 90 3
02 95 1
03 91 2
04 70 5
05 82 4
*/
*,
(select count(1) from 表 where stuScore >= t.stuScore) as stuRank
from 表 as t
你这个sql语句执行不了啊!
MySQL中的ROWNUM的实现
'截至2010-08-08 23:42:02 用户结帖率0.00% 当您的问题得到解答后请及时结贴.
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html8、如何给分和结贴?
http://community.csdn.net/Help/HelpCenter.htm#结帖