studentNo  pass
A          語文
A          數學
A          英語
A          地理
B          語文
B          數學
B          英語因為學生A過了4門課,所以給他的科目分別加上1234的序號
B學生過了3門,所以給他的科目加上123的序號,
效果如下:NO studentNo  pass
1  A          語文
2  A          數學
3  A          英語
4  A          地理
1  B          語文
2  B          數學
3  B          英語請問在Mysql的查詢中如何得到這樣的結果?多謝。

解决方案 »

  1.   

    先增加no字段
    update tb set no =1 where pass='語文'
    update tb set no =2 where pass='數學'
    update tb set no =3 where pass='英語'
    update tb set no =4 where pass='地理'
      

  2.   

    查询的话就是
    select case 
    when pass='語文' then 1
    when pass='數學' then 2
    when pass='英語' then 3
    when pass='地理' then 4
    end as no
    studentNo,pass  from tb
      

  3.   

    先增加no字段 
    update tb set no =1 where pass='語文' 
    update tb set no =2 where pass='數學' 
    update tb set no =3 where pass='英語' 
    update tb set no =4 where pass='地理'select count(tb1.*)as no,tb1.studentNo,tb1.pass from tb as tb1,tb as tb2 where 
    tb1.no<=tb2.no and tb2.studentNo=tb1.studentNo
      

  4.   

    set @i=0; 
    select stu.* from (select @i:=@i+1 as i,studentNo from student) stu,
    (select count(*) c, studentNo from student group by studentNo ) b where stu.i<=b.c and stu.studentNo=b.studentNo;
      

  5.   

    set @i=0; 
    select a.* , d.i
    from student a,(select stu.* from (select @i:=@i+1 as i,studentNo,pass from student) stu,
    (select count(*) c, studentNo from student group by studentNo ) b where stu.studentNo=b.studentNo) d
    where a.studentNo=d.studentNo and a.pass =d.pass;
      

  6.   

    select (select count(*) from tbl1 where studentNo=t.studentNo and pass <=t.pass) as `NO` ,studentNo,pass
    from tbl1 t;