【踩坑】mysql LIMIT 与 ORDER BY位置问题

今天在重做iblog项目后台时,当测试功能,发现后台报错,位置在 MyBatis 向 MySQL 取数据时的语法问题,如下:

SELECT *         FROM article         WHERE authorID = 1         LIMIT 0, 10000         ORDER BY articleTime                       DESC

然后我用 phpMyAdmin 测试时,发现错误定位在 LIMIT 字眼,删去后才能正常获取数据,这是为什么呢?

然后我把 LIMIT 放指令最后,发现取数据正常,这又是为什么呢?难度 LIMIT 什么时候都要放最后?

经过谷歌一番,有网友解答了:

If you use LIMIT row_count with ORDER BY, MySQL ends the sorting as soon as it has found the first row_count rows of the sorted result, rather than sorting the entire result.

反正遇到 ORDER BY 一定要把 LIMIT 语句放最后。

posted @ 2019-04-17 09:37  lipohong  阅读(513)  评论(0编辑  收藏  举报