mysql怎么指定查询一张表的查询结果,如最后5行记录和最前5行记录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql怎么指定查询一张表的查询结果,如最后5行记录和最前5行记录相关的知识,希望对你有一定的参考价值。
参考技术A mysql如何指定查询一张表的查询结果,如最后5行记录和最前5行记录mysql如何指定查询一张表的查询结果,如最后5行记录和最前5行记录
我们以student表为例,里面有三个字段:id,name,age,其中id为主健,为自增,里面共有10条记录,如下所示。
mysql> select * from student;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | li | 11 |
| 2 | zh | 12 |
| 3 | chou | 13 |
| 4 | he | 14 |
| 5 | lin | 15 |
| 6 | ll | 16 |
| 7 | chen | 17 |
| 8 | yu | 18 |
| 9 | wu | 19 |
| 10 | xie | 20 |
+----+------+------+
10 rows in set (0.00 sec)
1、查询第一行记录
select * from student limit 1;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | li | 11 |
+----+------+------+
1 row in set (0.00 sec)
2、查询最后一行记录
select * from student order by id desc limit 1;
+----+------+------+
| id | name | age |
+----+------+------+
| 10 | xie | 20 |
+----+------+------+
1 row in set (0.00 sec)
3、查询前n行记录,如前5行
select * from student limit 5;
select * from student limit 0,5;
select * from student order by id asc limit 5;
上面三条语句的结果都是一样的,如下:
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | li | 11 |
| 2 | zh | 12 |
| 3 | chou | 13 |
| 4 | he | 14 |
| 5 | lin | 15 |
+----+------+------+
5 rows in set (0.00 sec)
4、查询后n行记录,如后5条,注意结果为倒序排序,因为用了desc
select * from student order by id desc limit 5;
+----+------+------+
| id | name | age |
+----+------+------+
| 10 | xie | 20 |
| 9 | wu | 19 |
| 8 | yu | 18 |
| 7 | chen | 17 |
| 6 | ll | 16 |
+----+------+------+
5 rows in set (0.00 sec)
5、查询第m行到第n行记录,注意表中的记录下标是从0开始的,就像数组一样
select * from student limit m,n; 返回m+1到m+n行记录,m代表开始的下标,n代表查找的结果数,将返回n行结果
select * from student limit 2,8; 返回3到10行记录
+----+------+------+
| id | name | age |
+----+------+------+
| 3 | chou | 13 |
| 4 | he | 14 |
| 5 | lin | 15 |
| 6 | ll | 16 |
| 7 | chen | 17 |
| 8 | yu | 18 |
| 9 | wu | 19 |
| 10 | xie | 20 |
+----+------+------+
8 rows in set (0.00 sec)
select * from student limit 3,1; 返回第4行
+----+------+------+
| id | name | age |
+----+------+------+
| 4 | he | 14 |
+----+------+------+
1 row in set (0.00 sec)
6、查询一条记录($id)的下一条记录
select * from student where id>$id order by id asc limit 1;
如$id=4时将返回第5条记录
select * from student where id>4 order by id asc limit 1;
+----+------+------+
| id | name | age |
+----+------+------+
| 5 | lin | 15 |
+----+------+------+
1 row in set (0.00 sec)
7、查询一条记录($id)的上一条记录
select * from student where id<$id order by id desc limit 1;
如$id=4时将返回第3条记录
select * from student where id<4 order by id desc limit 1;
+----+------+------+
| id | name | age |
+----+------+------+
| 3 | chou | 13 |
+----+------+------+
1 row in set (0.00 sec)本回答被提问者和网友采纳
SQL怎么把查询的结果插入到另一张表中,两张表的表结构不一样可以吗?
您好,可以的 用 insert into select例,吧Table1 的 c,d 两列查询结果,插入到 Table2 的,a,b 两列
Insert into Table2(a,b) select c,d from Table1 参考技术A 使用inner join 关键字。例如一张表是学生信息表(studentlist),里面是姓名(Name),学号StudentID,出生日期Birthday这些。而另一张表是成绩信息表(scoreinfo),里面是班级ClassID,科目,教师,成绩Score这些,现在需要把这两张表里的学号,姓名,班级,成绩查到另一张表中,可以用以下SQL语句:
select StudentID,Name,ClassID,Score from StudentList inner join ScoreInfo on StudentList.id=ScoreInfo.StudentID
希望对你有帮助,我刚把数据库这些学完,有兴趣可以进一步交流
以上是关于mysql怎么指定查询一张表的查询结果,如最后5行记录和最前5行记录的主要内容,如果未能解决你的问题,请参考以下文章
sql查询:使用内连接查询两张表的时候,如果左边表的一条记录对应了右边表的两条记录,结果显示排列问题