子查詢和高效分頁

2022-03-28 10:56:21 字數 936 閱讀 6747

select id from classes where name ='一期'

select* from students where cid =(select id from classes where name ='一期')--子查詢

select * from students where cid in (select id from classes where name ='一期'

select * from students where cid not in (select id from classes where name ='一期')

select * from students where exists (select * from classes where classes.id=students .cid) --主查詢會根絕子查詢的條件進行匹配

select * from students where not exists (select * from classes where classes.id=students .cid)

--第一種分頁方式,如果要取很多頁之後的資料效能比較低

select top 10 * from students where id in (select top 10 id from students )--查出前十條資料

select top 10 * from students where id not in (select top 10 id from students )--查出第二頁資料

--第二種分頁比較高效,row_number()方法會給結果集編號

select * from (

select *,row_number()over(order by id) as rowsnumber from students ) as t

where t.rowsnumber between 1 and 10

高效的MySQL分頁 利用子查詢分頁

先抄回來 首先看一下分頁的基本原理 mysql explain select from message order by id desc limit 10000,20g 1.row id 1 select type table message type index possible keys nul...

sql連線查詢,子查詢和分頁查詢

內連線查詢,有效的去除笛卡爾積,分兩種 根據所使用的比較方式不同,內連線分為等值連線 自然連線和自連線三種 舉乙個自連線例子 select e.name,m.name from employees e join employees m on e.id m.id 分為左外連線和右外連線,用於查詢乙個表...

SQL SERVER2005高效分頁查詢

sqlserver2005不支援關鍵字limit 所以它的分頁sql查詢語句將不能用mysql的方式進行,幸好sqlserver2005提供了top,rownumber等關鍵字,這樣就能通過這幾個關鍵字實現分頁。幾種sqlserver2005高效分頁sql查詢語句 top方案 select top ...