要按块检索大量选择,您需要使用order by limit。语法如下:
select *from yourtablename order by yourcolumnname limit 0,10;
从上面的语法中,你将从表中获取10行。在上述语法中,0代表表的结果集中的第一行,这意味着它是基于零索引的。limit的第二个值表示可以从表中检索的最大行数。
如果你想获取10到30之后的行,那么在limit中使用如下语法:
select *from yourtablename order by yourcolumnname limit 10,20; //11 to 30
如果您想要另一组从 30 到 50 的行,请再次使用 limit:
select *from yourtablename order by yourcolumnname limit 30 ,20; 31 to 50.
为此,您需要使用临时表。语法如下:
drop temporary table if exists yourtemporarytablename;create temporary table yourtemptablenameas( select *from youroriginaltablename order by limit 0,100);
如果所有记录都在临时表中,则根据上面讨论的使用limit从临时表中获取所有记录。
select *from yourtemporarytablename limit 0,100;select *from yourtemporarytablename limit 100,1000;
现在,由您来设置限制值。现在删除临时表是一个很好的做法。查询如下:
drop temporary table yourtemporarytablename;
让我们来演示上面的讨论。首先创建一个表。创建表的查询如下:
mysql> create table getrecordsdemo -> ( -> id int not null auto_increment, -> primary key(id) -> );query ok, 0 rows affected (1.68 sec)
使用插入命令在表中插入一些记录。查询如下:
mysql> insert into getrecordsdemo values(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),();query ok, 738 rows affected (0.34 sec)records: 738 duplicates: 0 warnings: 0
现在创建一个像上表一样的临时表。创建临时表的查询如下:
mysql> drop temporary table if exists temprecord;query ok, 0 rows affected, 1 warning (0.00 sec)mysql> create temporary table temprecord -> as -> ( -> select * from getrecordsdemo order by id limit 0,738 -> );query ok, 738 rows affected (0.03 sec)records: 738 duplicates: 0 warnings: 0
现在您可以使用limit子句以块的形式获取结果。
案例1:以下查询用于从临时表‘temprecord’中获取一些记录:
mysql> select *from temprecord limit 0,10;
以下是输出结果:
+----+| id |+----+| 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 10 |+----+10 rows in set (0.00 sec)
情况2:查询如下以获取下一组记录:
mysql> select *from temprecord limit 10,20;+----+| id |+----+| 11 || 12 || 13 || 14 || 15 || 16 || 17 || 18 || 19 || 20 || 21 || 22 || 23 || 24 || 25 || 26 || 27 || 28 || 29 || 30 |+----+20 rows in set (0.00 sec)
情况3:查询如下,获取另一组记录:
mysql> select *from temprecord limit 30,20;+----+| id |+----+| 31 || 32 || 33 || 34 || 35 || 36 || 37 || 38 || 39 || 40 || 41 || 42 || 43 || 44 || 45 || 46 || 47 || 48 || 49 || 50 |+----+20 rows in set (0.00 sec)
以上就是在 mysql 中按块检索大的选择?的详细内容。