一个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
应该怎么做呢?

解决方案 »

  1.   

    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);
      

  2.   

    select * from score;
    query result(3 records)
    id name score place 
    1 aaa 95 2 
    2 aaa 100 1 
    3 aaa 90 3 
      

  3.   

    谢谢 yueliangdao0608((深圳PHPER,MSN:[email protected]))!我只是假设3条记录,如果是成千上万条呢?用case when不大合适吧?
      

  4.   

    那你就在程序里判断,把成千上万的记录先select出来,然后分析这些数据。
      

  5.   

    目前我是在程序里(PHP代码)这样判断的,现select,然后处理,然后更新place字段。想看看有没有更好的方法。
      

  6.   

    一条SQL恐怕不能实现,不能在对一张表update的同时select。写个存储过程吧
      

  7.   

    to wasuka(萝莉控):能大概写一下SQL代码吗?
      

  8.   

    或者,在程序中(PHP代码)实现,有没有更好的办法呢?