Retrieving only a fixed number of rows in MySQL

后端 未结 6 1412
时光取名叫无心
时光取名叫无心 2020-12-03 07:55

I am testing my database design under load and I need to retrieve only a fixed number of rows (5000)

I can specify a LIMIT to achieve this, however it seems that th

6条回答
  •  独厮守ぢ
    2020-12-03 08:09

    As I explained in this article, each database defines its own ay of limiting the result set size depends on the database you are using.

    While the SQL:2008 specification defines a standard syntax for limiting a SQL query, MySQL 8 does not support it.

    Therefore, on MySQL, you need to use the LIMIT clause to restrict the result set to the Top-N records:

    SELECT
        title
    FROM
        post
    ORDER BY
        id DESC
    LIMIT 50
    

    Notice that we are using an ORDER BY clause since, otherwise, there is no guarantee which are the first records to be included in the returning result set.

提交回复
热议问题