0

I want to delete specific variables based on 'id' value. but the code below is displaying syntax error near: OFFSET 1. I use a similar code where I use SELECT instead of DELETE and it works fine, What am doing wrong here? Thanks

DELETE  FROM users WHERE name = '$name' ORDER BY id ASC LIMIT 1 OFFSET 1
user1559811
  • 437
  • 3
  • 10
  • 26

4 Answers4

0

You cannot specify offset in DELETE's LIMIT clause.

Simple use:

DELETE  FROM users WHERE name = '$name' ORDER BY id ASC LIMIT 1;
metalfight - user868766
  • 2,722
  • 1
  • 16
  • 20
0

Try

DELETE  FROM users WHERE name = '$name' ORDER BY id ASC LIMIT 1,1
Madhivanan
  • 13,470
  • 1
  • 24
  • 29
0

You can not have OFFSET in DELETE query.

jsist
  • 5,223
  • 3
  • 28
  • 43
0

The offset component in LIMIT is not available in MySQL DELETE statements but it is allowed in SELECT statements.

So what you can do to get around this fact, is you can actually join a subselect in a DELETE operation, which will then give you your desired results:

DELETE a FROM users a
INNER JOIN
(
    SELECT id
    FROM users
    WHERE name = '$name'
    ORDER BY id
    LIMIT 1,1
) b ON a.id = b.id
Zane Bien
  • 22,685
  • 6
  • 45
  • 57