我的SQL查询出了什么问题?

时间:2016-06-27 14:06:22

标签: mysql sql laravel orm eloquent

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

我收到了这条消息:

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

我不是sql专家,但我真的无法弄清楚偏移是怎么回事。

顺便说一下,这个查询是由Eloquent ORM根据以下代码生成的:

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

我刚刚将结果查询打入PHPMyAdmin以检查发生了什么,这就是我所拥有的

你知道怎么了? PHPMyAdmin荧光笔甚至没有突出显示偏移关键字。

提前致谢

1 个答案:

答案 0 :(得分:3)

MySQL syntax requires LIMIT x before OFFSET x.

语法:

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

它必须是这样的:

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