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)
hql分頁(獲取從第m到第n條資料)
hibernate封裝了sql語句成了hql,雖然對於多表級聯查詢顯得很麻煩甚至不如sql,但是其實用慣了的話,對於一些常規資料的查詢還是很方便的,比較自動給封裝到物件裡了,不像sql那般麻煩。那麼下面就解決一下hql分頁顯示問題,也就是只取結果集中某一段的結果,怎麼寫呢?其實很簡單,hql的que...
hql分頁(獲取從第m到第n條資料)
hibernate封裝了sql語句成了hql,雖然對於多表級聯查詢顯得很麻煩甚至不如sql,但是其實用慣了的話,對於一些常規資料的查詢還是很方便的,比較自動給封裝到物件裡了,不像sql那般麻煩。那麼下面就解決一下hql分頁顯示問題,也就是只取結果集中某一段的結果,怎麼寫呢?其實很簡單,hql的que...
mysql 查詢分組裡的第n條資料,前n條資料
select uname,salary,address,new rank from select uname,salary,address,如果臨時變數等於 address,就 1,否則從1開始 if tmpaddress address,rank rank 1 rank 1 as new rank...