亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

MySQL LIMIT使用介紹

標簽:
MySQL

Summaryin this tutorial, you will learn how to use MySQL LIMIT clause to select records from the beginning, middle and end of a result set.

MySQL LIMIT syntax

The LIMIT clause is used in the SELECT statement to constrain the number of rows in a result set. The LIMITclause accepts one or two arguments. The values of both arguments must be zero or positive integer constants.

The following illustrates the LIMIT clause syntax with 2 arguments:

SELECT * FROM tbl LIMIT offset, count

Let’s see what the offset and count mean in the LIMIT clause:

  • The offset specifies the offset of the first row to return. The offset of the first row is 0, not 1.

  • The count specifies maximum number of rows to return.

When you use LIMIT with one argument, this argument will be used to specifies the maximum number of rows to return from the beginning of the result set.

SELECT * FROM tbl LIMIT count

The query above is equivalent to the following query with the LIMIT clause that accepts two arguments:

SELECT * FROM tbl LIMIT 0, count

 

Using MySQL LIMIT to get the first N rows

You can use the LIMIT clause to select the first N rows in a table as follows:

SELECT * FROM tbl LIMIT N

For example, to select the first 10 customers, you use the following query:

SELECT customernumber,        customername,        creditlimit FROM customers LIMIT 10;

mysql limit first n rows

Using MySQL LIMIT to get the highest and lowest values

The LIMIT clause often used with ORDER BY clause. First, you use the ORDER BY clause to sort the result set based on a certain criteria, and then you use LIMIT clause to find lowest or highest values.

For example, to select 5 customers who have the highest credit limit, you use the following query:

SELECT customernumber,        customername,    creditlimit FROM customers ORDER BY creditlimit DESC LIMIT 5;

mysql limit get highest values

And the following query returns 5 customers who have the lowest credit limit:

SELECT customernumber,        customername,        creditlimit FROM customers ORDER BY creditlimit ASC LIMIT 5;

mysql limit get lowest values

Using MySQL LIMIT to get the N highest values

One of the toughest questions in MySQL is how to select the N highest values in a result set e.g., select the second most expensive product, which you cannot use MAX or MIN functions to answer. However, you can use MySQL LIMIT to answer those kinds of questions.

Let’s take a look at the products result set of the following query:

SELECT productName,        buyprice FROM products ORDER BY buyprice DESC;

MySQL LIMIT products table

Our task is to get the highlight product, which is the second most expensive product in the products result set. In order to do so, you use LIMIT clause to select 1 row from the second row as the following query: (notice that the offset starts from zero)

SELECT productName,    buyprice FROM products ORDER BY buyprice DESC LIMIT 1, 1

mysql limit most second expensive product

In this tutorial, you have learned how to use MySQL LIMIT clause to select records from the beginning, the middle and the end of a result set.

Related Tutorials

原文链接:http://outofmemory.cn/mysql/mysql-limit

點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消