0

Say I have 20 records, and I want to update row 6 to row 10. I have tried this code but getting an error:

UPDATE db_scheduling.tbl_student 
  SET section ='a' 
  WHERE yrlevel = '1' 
  ORDER BY grade DESC 
  LIMIT 5 
  OFFSET 5;

It says I have error on the OFFSET part and I don't know if it works in UPDATE query. When I remove the OFFSET 5, it just updates the first 5 rows. Help please.

Supravat Mondal
  • 2,574
  • 2
  • 22
  • 33
Lee Song
  • 83
  • 2
  • 12
  • possible duplicate of [update multiple rows using limit in mysql?](http://stackoverflow.com/questions/1513206/update-multiple-rows-using-limit-in-mysql) – Strawberry Apr 30 '15 at 05:22

2 Answers2

1

you can use something like this:

UPDATE db_scheduling.tbl_student  
    SET section ='a'
    WHERE yrlevel IN (
     SELECT id FROM (
         SELECT yrlevel FROM db_scheduling.tbl_student 
         ORDER BY grade DESC  
         LIMIT 5, 5
     ) tmp
    );

Have a look update multiple rows using limit in mysql?

Community
  • 1
  • 1
Supravat Mondal
  • 2,574
  • 2
  • 22
  • 33
0

You can also use a self join with only rows that you want to update

UPDATE `tbl_student` s
JOIN (
SELECT id FROM `tbl_student` 
WHERE yrlevel = '1' 
  ORDER BY grade DESC 
  LIMIT 5 
  OFFSET 5
) s1 USING(id)
SET s.section ='a' 

DEMO

M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118