SQL的各種常用演算法問題

2021-04-21 01:13:36 字數 1880 閱讀 7441

---------------------------------------------

分頁演算法:

---------------------------------------------

實現分頁,可以使用巢狀查詢,也可以使用儲存過程。不過儲存過程太複雜,執行效率也不一定高,

所以我就用這樣的巢狀語句來實現:

select top 頁大小 * from testtable

where (id >(select max(id) from (select top 頁大小*頁數 id  from testtable order by id) as t)) order by id

select * from (select * from scott.emp order by scott.emp.empno) where rownum<=20;

select a.empno from (select * from scott.emp where rownum<=20 order by scott.emp.empno ) a

minus

select b.empno from (select * from scott.emp where rownum<=10 order by scott.emp.empno) b ;

select a.empno from (select * from scott.emp where rownum<=20 order by scott.emp.empno ) a

union all

select b.empno from (select * from scott.emp where rownum<=10 order by scott.emp.empno) b ;

select a.empno from (select * from scott.emp where rownum<=20 order by scott.emp.empno) a

minus

select b.empno from scott.emp b

select a.empno from (select * from scott.emp where rownum<=20 order by scott.emp.empno) a

minus

select b.empno from scott.emp b  where rownum<=10 ;

select a.empno from (select * from scott.emp where rownum<=20 order by scott.emp.empno) a

intersect

select b.empno from (select * from scott.emp where rownum<=10 order by scott.emp.empno) b ;

select aa.empno from ( select * from (select * from scott.emp order by scott.emp.empno) a where rownum<=20 ) aa

minus

select bb.empno from ( select * from (select * from scott.emp order by scott.emp.empno) a where rownum<=10 ) bb

select a.empno from (select scott.emp.empno from scott.emp where rownum<=20 order by scott.emp.empno) a

minus

select b.empno from (select scott.emp.empno from scott.emp where rownum<=10 order by scott.emp.empno) b;

sql的各種問題

1.case遇到符合條件的表示式就返回,即只有乙個返回值。例如 select sal,case whenempno 7369 then when sal 1000 then 低 when sal 2000 then 較低 when sal 3000 then 中 when sal 4000 then...

SQL各種寫法的效率問題

問 1 一次插入多條資料時 create table tb id int,名稱nvarchar 30 備註nvarchar 1000 insert tbselect 1,ddd 1 union allselect 1,5100 d union allselect 1,5200 e 也可以這樣 cre...

SQL各種寫法的效率問題

問 1 一次插入多條資料時 create table tb id int,名稱nvarchar 30 備註nvarchar 1000 insert tbselect 1,ddd 1 union allselect 1,5100 d union allselect 1,5200 e 也可以這樣 cre...