DeadlyBacon DeadlyBacon - 1 year ago 56
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:

->orderBy('updated_at', 'desc')
->offset(Config::PAGE_SIZE * Config::MAX_PAGES)

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 Source

MySQL syntax requires LIMIT x before OFFSET x.


[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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download