重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这个很简单。你在查询的时候要查3次,第一次查询的是当前的记录,第2,3次查询的分别是上一条和下一条记录。
成都创新互联是一家专业提供平山企业网站建设,专注与成都做网站、网站制作、成都外贸网站建设、H5页面制作、小程序制作等业务。10年已为平山众多企业、政府机构等服务。创新互联专业网站建设公司优惠进行中。
所以说应该是3条语句。
select * from news where id=ID
select top 1 * from news where id ID and classid=classid order by id asc
select top 1 * from news where id ID and classid=classid order by id desc
你可能要先把当前的记录查出来,取出这条记录的分类ID,然后才能写出下面的2条查询语句。
mysql 与 msql一样,都用Transact-SQL语句查询!
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 id4 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 id4 order by id desc limit 1;
+----+------+------+
| id | name | age |
+----+------+------+
| 3 | chou | 13 |
+----+------+------+
1 row in set (0.00 sec)
select * from userlist order by id desc limit 5;
说明:id是自增id的情况下用这个,当然也可用插入时间来排序例如这样:
select * from userlist order by created_at desc limit 5;
按现在的正序查的最后十条 就是倒序排的前十条啊,
倒序排 limit 10 就好了
你这多个主键的话就是求合集了
SELECT TOP 100 * FROM TABLE WHERE A=2 AND B=10
1、创建测试表,
create table test_limit(id int ,value varchar(100));
2、插入测试数据,共6条记录;
insert into test_limit values (1,'v1');
insert into test_limit values (2,'v2');
insert into test_limit values (3,'v3');
insert into test_limit values (4,'v4');
insert into test_limit values (5,'v5');
insert into test_limit values (6,'v6');
3、查询表中全量数据,可以发现共6条数据,select * from test_limit t;
4、编写语句,指定查询3条数据;
select * from test_limit limit 3;