MySQL Limit order by, mysqllimit
When I write a fuzzy query today and sort by time and pagination, there is such an SQL statement in the ing file of mybatis.
1 SELECT 2 <include refid="Base_Column_List"/> 3 FROM USER U 4 WHERE U.status != #{status,jdbcType=VARCHAR} 5 <if test="keyword != null and keyword != ''"> 6 AND 7 ( 8 U.NAME LIKE #{keyword,jdbcType=VARCHAR} 9 OR U.USERNAME LIKE #{keyword,jdbcType=VARCHAR}10 )11 </if>12 ORDER BY U.lastupdateddate DESC13 LIMIT #{offsetNum},#{pageSize}
If the results are sorted first and then paged, you can get the desired results, but vice versa.
SELECT <include refid="Base_Column_List"/> FROM USER U WHERE U.status != #{status,jdbcType=VARCHAR} <if test="keyword != null and keyword != ''"> AND ( U.NAME LIKE #{keyword,jdbcType=VARCHAR} OR U.USERNAME LIKE #{keyword,jdbcType=VARCHAR} ) </if> LIMIT #{offsetNum},#{pageSize} ORDER BY U.lastupdateddate DESC
In this case, an error is reported. The SQL Execution sequence is from where select. Can I think that, under the where condition with the same priority, who is in the front will give priority to execution, while order is the sorting of query results, limit is the selective acquisition of query results; if limit is in the front, the better results are after selective acquisition
The result is then sorted. Whether the result can be obtained is related to the order by working mechanism. According to the above example, we can only sort and then selectively obtain the results. Thank you very much if you can post an explanation in the official document ~