根据排序更新某个字段

求助:根据排序更新某个字段

一个table,有如下四个字段:id、name、score和place,其中id是auto_increment的,score代表分数,place代表名次。

现在,表中所有的记录的score字段都有值,place字段的都是默认的0。

我想,根据score字段的值,更性place字段的值。

比如,假设有3个记录:

id         name         score         place

———————————-

1           ‘aaa ‘           95               0

2           ‘bbb ‘           100             0

3           ‘ccc ‘           90               0

想改为:

id         name         score         place

———————————-

1           ‘aaa ‘           95               2

2           ‘bbb ‘           100             1

3           ‘ccc ‘           90               3

应该怎么做呢?



create table score(

id int(11) auto_increment primary key,

name varchar(64),

score int(11),

place int(2)

)

insert into score select 1, ‘aaa ‘,95,0;

insert into score select 2, ‘aaa ‘,100,0;

insert into score select 3, ‘aaa ‘,90,0;

update score set place = (case score when 95 then 2 when 100 then 1 when 90 then 3 end);



一条SQL恐怕不能实现,不能在对一张表update的同时select。

写个存储过程吧

根据排序更新某个字段

相关文章:

你感兴趣的文章:

标签云: