在mysql中,可以利用“limit”子句来限制查询个数,该子句可以限制select查询结果返回的条数,语法“select column1,column2,... from table limit offset , count;”。
本教程操作环境:windows7系统、mysql8版本、dell g3电脑。
当数据表中有上万条数据时,一次性查询出表中的全部数据会降低数据返回的速度,同时给数据库服务器造成很大的压力。这时就可以用 limit 子句来限制查询结果返回的条数。
limit 是 mysql 中的一个特殊关键字,用于指定查询结果从哪条记录开始显示,一共显示多少条记录。
1. mysql limit子句简介在select语句中使用limit子句来约束结果集中的行数。limit子句接受一个或两个参数。两个参数的值必须为零或正整数。
下面说明了两个参数的limit子句语法:
select column1,column2,...from tablelimit offset , count;
我们来查看limit子句参数:
offset参数指定要返回的第一行的偏移量。第一行的偏移量为0,而不是1。count指定要返回的最大行数。
当您使用带有一个参数的limit子句时,此参数将用于确定从结果集的开头返回的最大行数。
select column1,column2,...from tablelimit count;
上面的查询等同于下面接受两个参数的limit子句的查询:
select column1,column2,...from tablelimit 0 , count;
2. 使用mysql limit获取前n行可以使用limit子句来选择表中的前n行记录,如下所示:
select column1,column2,...from tablelimit n;
例如,要查询employees表中前5个客户,请使用以下查询:
select customernumber, customername, creditlimit from customers limit 5;
或者 -
select customernumber, customername, creditlimit from customers limit 0,5;
执行上面语句,得到以下结果 -
mysql> select customernumber, customername, creditlimit from customers limit 5;+----------------+----------------------------+-------------+| customernumber | customername | creditlimit |+----------------+----------------------------+-------------+| 103 | atelier graphique | 21000 || 112 | signal gift stores | 71800 || 114 | australian collectors, co. | 117300 || 119 | la rochelle gifts | 118200 || 121 | baane mini imports | 81700 |+----------------+----------------------------+-------------+5 rows in set
3. 使用mysql limit获得最高和最低的值limit子句经常与order by子句一起使用。首先,使用order by子句根据特定条件对结果集进行排序,然后使用limit子句来查找最小或最大值。
注意:order by子句按指定字段排序的使用。
请参见示例数据库(yiibaidb)中的以下customers表,其表结构如下所示 -
mysql> desc customers;+------------------------+---------------+------+-----+---------+-------+| field | type | null | key | default | extra |+------------------------+---------------+------+-----+---------+-------+| customernumber | int(11) | no | pri | null | || customername | varchar(50) | no | | null | || contactlastname | varchar(50) | no | | null | || contactfirstname | varchar(50) | no | | null | || phone | varchar(50) | no | | null | || addressline1 | varchar(50) | no | | null | || addressline2 | varchar(50) | yes | | null | || city | varchar(50) | no | | null | || state | varchar(50) | yes | | null | || postalcode | varchar(15) | yes | | null | || country | varchar(50) | no | | null | || salesrepemployeenumber | int(11) | yes | mul | null | || creditlimit | decimal(10,2) | yes | | null | |+------------------------+---------------+------+-----+---------+-------+13 rows in set
例如,要查询信用额度最高的前五名客户,请使用以下查询:
select customernumber, customername, creditlimitfrom customersorder by creditlimit desclimit 5;
执行上面查询语句,得到以下结果 -
mysql> select customernumber, customername, creditlimitfrom customersorder by creditlimit desclimit 5;+----------------+------------------------------+-------------+| customernumber | customername | creditlimit |+----------------+------------------------------+-------------+| 141 | euro+ shopping channel | 227600 || 124 | mini gifts distributors ltd. | 210500 || 298 | vida sport, ltd | 141300 || 151 | muscle machine inc | 138500 || 187 | av stores, co. | 136800 |+----------------+------------------------------+-------------+5 rows in set
以下查询将返回信用限额最低的五位客户:
select customernumber, customername, creditlimitfrom customersorder by creditlimit asclimit 5;
4. 使用mysql limit获得第n个最高值mysql中最棘手的问题之一是:如何获得结果集中的第n个最高值,例如查询第二(或第n)贵的产品是哪个,显然不能使用max或min这样的函数来查询获得。 但是,我们可以使用mysql limit来解决这样的问题。
首先,按照降序对结果集进行排序。第二步,使用limit子句获得第n贵的产品。通用查询如下:
select column1, column2,...from tableorder by column1 desclimit nth-1, count;
下面我们来看看一个例子,将使用示例数据库(yiibaidb)中的产品(products)表来进行演示。products表的结构如下所示 -
mysql> desc products;+--------------------+---------------+------+-----+---------+-------+| field | type | null | key | default | extra |+--------------------+---------------+------+-----+---------+-------+| productcode | varchar(15) | no | pri | null | || productname | varchar(70) | no | | null | || productline | varchar(50) | no | mul | null | || productscale | varchar(10) | no | | null | || productvendor | varchar(50) | no | | null | || productdescription | text | no | | null | || quantityinstock | smallint(6) | no | | null | || buyprice | decimal(10,2) | no | | null | || msrp | decimal(10,2) | no | | null | |+--------------------+---------------+------+-----+---------+-------+9 rows in set
查看以下产品表中的行记录:
mysql> select productcode, productname, buypricefrom productsorder by buyprice desc;+-------------+--------------------------------------+----------+| productcode | productname | buyprice |+-------------+--------------------------------------+----------+| s10_4962 | 1962 lanciaa delta 16v | 103.42 || s18_2238 | 1998 chrysler plymouth prowler | 101.51 || s10_1949 | 1952 alpine renault 1300 | 98.58 || s24_3856 | 1956 porsche 356a coupe | 98.3 || s12_1108 | 2001 ferrari enzo | 95.59 || s12_1099 | 1968 ford mustang | 95.34 |... ....+-------------+--------------------------------------+----------+110 rows in set
我们的任务找出结果集中价格第二高的产品。可以使用limit子句来选择第二行,如以下查询(注意:偏移量从0开始,所以要指定从1开始,然后取一行记录):
select productcode, productname, buyprice from productsorder by buyprice desclimit 1, 1;
执行上面查询语句,得到以下结果 -
mysql> select productcode, productname, buyprice from productsorder by buyprice desclimit 1, 1;+-------------+--------------------------------+----------+| productcode | productname | buyprice |+-------------+--------------------------------+----------+| s18_2238 | 1998 chrysler plymouth prowler | 101.51 |+-------------+--------------------------------+----------+1 row in set
类似的,获取售价第三高、第四高的产品信息为:limit 2, 1 和 limit 3, 1。
【相关推荐:mysql视频教程】
以上就是mysql怎么限制查询个数的详细内容。