MySQL limit from descending order

前端 未结 4 1388
死守一世寂寞
死守一世寂寞 2020-12-30 23:56

Is it available to write a query to use same \"LIMIT (from), (count)\", but get result in backwards?

In example if I have 8 rows in the table and I want to get 5 row

4条回答
  •  情话喂你
    2020-12-31 00:23

    No, you shouldn't do this. Without an ORDER BY clause you shouldn't rely on the order of the results being the same from query to query. It might work nicely during testing but the order is indeterminate and could break later. Use an order by.

    SELECT * FROM table1 ORDER BY id LIMIT 5
    

    By the way, another way of getting the last 3 rows is to reverse the order and select the first three rows:

    SELECT * FROM table1 ORDER BY id DESC LIMIT 3
    

    This will always work even if the number of rows in the result set isn't always 8.

提交回复
热议问题