您的位置:首页 > 数据库

sql基础--怎样让查询出来的数据只返回多少行

2016-04-05 23:53 513 查看
sql基础--怎样让查询出来的数据只返回多少行

limit 关键字,可以限制检索出来的数据,只返回多少行,也是这个意思,就是告诉数据库,返回给我的结果不要超过2行数据

mysql> select name from products limit 2;

+-------------------+

| name |

+-------------------+

| Bird bean bag toy |

| qunkanlu |

+-------------------+

2 rows in set (0.01 sec)

注意 limit 后面还可以加 offset关键字 表示指定从哪行开始返回结果给我。

警告: offset 是从0行开始算起的哈,不是从1行开始算的。

实验:

我这里有4条数据,

mysql> select * from products;

+------+-------+--------------------+

| id | price | name |

+------+-------+--------------------+

| 1 | 3.49 | Bird bean bag toy |

| 2 | 3.49 | qunkanlu |

| 3 | 4.9 | qunkanlu 50 number |

| 1 | 5.9 | hongqi |

+------+-------+--------------------+

把 offset 1 为1的时候返回的情况:



mysql> select name from products limit 1 offset 1;

+----------+

| name |

+----------+

| qunkanlu |

+----------+

是从第二条群康路 开始检索的。limit 是要求它只是返回1行数据。

是从第三条群康路50号 开始检索的。limit
是要求它只是返回1行数据。

offset 2:

mysql> select name from products limit 1 offset 2;

+--------------------+

| name |

+--------------------+

| qunkanlu 50 number |

注意

limit适用于 mysql
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: