【问题标题】:What's so wrong with my SQL query?我的 SQL 查询出了什么问题?
【发布时间】:2016-10-29 14:14:22
【问题描述】:
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 专家,但我真的无法弄清楚 offset 有什么问题。

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

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

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

你们知道怎么回事吗? PHPMyAdmin 荧光笔甚至没有突出显示 offset 关键字。

提前致谢

【问题讨论】:

    标签: mysql sql laravel orm eloquent


    【解决方案1】:

    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;
    

    【讨论】:

    • 如果你像我一样是 sql 新手,这有点违反直觉。
    猜你喜欢
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多