简体   繁体   中英

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

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM