First three Groups with Highest Marks should have specific points

前端 未结 3 1344
谎友^
谎友^ 2020-12-21 15:59

my table

+------+-------+---------+-------+--------+
| Name | Group1| Section | Marks | Points |
+------+-------+---------+-------+--------+
| S1   | G1             


        
3条回答
  •  Happy的楠姐
    2020-12-21 16:29

    You can do an update join to achieve what you want;

    UPDATE students
    JOIN (
      SELECT marks, (@sp:=@sp-2) a
      FROM (
        SELECT distinct marks FROM students
        WHERE section='class1' AND group1 IS NOT NULL
        GROUP BY group1 ORDER BY marks DESC LIMIT 3
      ) b, (SELECT @sp:=7) c
    ) d
    SET students.points = d.a 
    WHERE students.marks = d.marks
      AND section='class1'
      AND group1 IS NOT NULL;
    

    (posted this answer also to your second question where you seemed to ask for an update statement without noticing that it was a duplicate, moved it here)

    An SQLfiddle to test with.

提交回复
热议问题