如何查詢資料庫表中倒數第5 10行記錄

時間 2022-07-26 07:52:23

1樓:

sql="select * from [temp] order by id desc limit 4,6"

這是mysql的寫法,不知道你用的是什麼資料庫啊。oracle的寫法就複雜了,要用到巢狀查詢

用sqlserver 的 top

select top n * from table_name where id not in (select top n id from table_name order by id desc) order by id desc

2樓:小旋風

這個是oracle的寫法,

如果查詢的表中沒有重覆記錄:

select a.* from 表 a where rownum<=(select max(rownum) from 表)-4

minus

select a.* from 表 a where rownum<=(select max(rownum) from 表)-10

如果查詢的表中有重覆記錄:

select a.表字段 from

(select a.*,rownum from 表 a where rownum<=(select max(rownum) from 表)-4

minus

select a.*,rownum from 表 a where rownum<=(select max(rownum) from 表)-10) a

怎麼按條件選取資料庫表中的倒數5-10位

3樓:匿名使用者

首先不存在倒數和正數的概念,因為:假設公升序(asc)是正數的話,那麼降序(desc)就是倒數,這一點可以用**控制。

好了,解決倒數的問題,剩下的就是5-10位的問題,很簡單,rownum大於5小於等於10就可以了。

對於你的例子,假如降序是正數的話:

select * from dianshiju where sname='張三' and dsjid not in(select top 5dsjid from dianshiju where name='張三') and rownum>5 and rownum<=10 order by id

4樓:衡修

什麼倒數5-10位?不明白,top 5 查詢出來,最多也就是5條資料。

mysql怎麼指定查詢一張表的查詢結果,如最後5行記錄和最前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)

查詢資料庫表中前10條記錄怎麼寫呢?

6樓:匿名使用者

查詢前十條記錄(順數)select top 10 * form 表 where ..." '也可以不要後面的where子句

查詢後十條記錄(倒數)select top 10 * form 表 where ... order by id desc"'注:id是資料庫自動編號

7樓:匿名使用者

select top 10 * from 表名

如何從SQL資料庫表中查詢倒數第三到倒數第六條記錄

在排序的時候用desc就是倒序排列.倒數第一就是第一條記錄.rs.absolute int 比如我要從第三條開始顯示則用rs.absolute 2 然後rs.next 就是第三條了.顯示倒第六條的時候break就可以了.use 資料庫名 godeclare totle int select totl...

資料庫中查詢優化的目的是什麼,資料庫中查詢優化的目的是什麼?

mrr 是 mysql 針對特定查詢的一種優化手段。假設乙個查詢有二級索引可用,讀完二級索引後要回表才能查到那些不在當前二級索引上的列值,由於二級索引上引用的主鍵值不一定是有序的,因此就有可能造成大量的隨機 io,如果回表前把主鍵值給它排一下序,那麼在回表的時候就可以用順序 io 取代原本的隨機 i...

如何將資料庫中的表複製到另資料庫中的表中

將乙個資料庫中的乙個表複製到另乙個資料庫中的表中 的過程如下。具體步驟 首先,開啟並連線sql server,在源資料庫source db 源資料庫名稱 上右鍵,然後依次點選 編寫表指令碼為 create到 新查詢編輯器視窗 在第1步產生的編輯器中按 crtl a 組合鍵全選內容,然後右鍵 複製 或...

sql怎麼把資料庫中的表移到另資料庫中

結構一樣的話 insert into 資料庫a.dbo.tablea select from 資料庫b.dbo.tablea 另外 nsert into ddd 欄位1,欄位2,欄位3 select 欄位1,欄位2,欄位3 from aaa,bbb,ccc 插入的字段和查詢的字段數量型別一致 由於你...

如何用T SQL命令查詢資料庫中有哪些表

select from sysobjects select name from sysobjects sqlserver如何用t sql命令查詢乙個資料庫中有哪些表 所有使用者表都存放在資料庫中的系統物件表sysobjects中。筆者以個人專用資料庫為例 select from sysobjects...