DeadlyBacon DeadlyBacon - 5 months ago 8
MySQL Question

What's so wrong with my SQL query?

select * from `a2_posts` where `reply_to` = -1 order by `updated_at` desc offset 4;


and I'm getting this message:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'offset 4' at line 1


I'm no sql expert but I really cant figure out what's so wrong with offset.

btw this query was generated by the Eloquent ORM, from this code:

Post::whereReplyTo($request->input('reply_to'))
->orderBy('updated_at', 'desc')
->offset(Config::PAGE_SIZE * Config::MAX_PAGES)
->get();


I just punched the resulting query into PHPMyAdmin to check what was going on and thats what I've got

Do you guys know what's wrong? the PHPMyAdmin highlighter didnt even highlight the offset keyword.

Thanks in advance

Edu Edu
Answer

MySQL syntax requires LIMIT x before OFFSET x.

Syntax:

[LIMIT {[offset,] row_count | row_count OFFSET offset}]

It needs to be something like this:

select * from `a2_posts` where `reply_to` = -1 
order by `updated_at` desc 
limit 2 offset 4;