一、分頁html
無論手機端需不須要分頁,我這個接口都通用,而且要查詢效率高
sql語句怎麼寫?pageNamber怎麼用?數據怎麼返回?sql
分頁策略一:先查詢出所有數據而後再分頁。getData?page=1網絡
mSublist=list.sublist(start,end);mSublist=list.sublist(30*(page-1)+1,30*page);若是要查詢所有 只須要start=end=0
而後返回這個mSublist 這樣的話須要寫一個分頁的工具類。若是數據量特別大,所有都查出來再分頁效率很低。oracle
分頁策略二:app
select * from table where row_num between 30*(pageNamber-1)+1 and 30*pageNambe;函數
pn=1 1-30
pn=2 31-60工具
SELECT * FROM(SELECT T.*,ROWNUM RN FROM U001 T) WHERE RN >=30*(pageNamber-1)+1 AND RN <= 30*pageNambe 這也是能夠的優化
果沒有分頁需求,這個不通用啊。寫兩個sql語句,若是pageNumber<1只寫所有查詢,不然按照第幾頁顯示url
orcale高效分頁:http://blog.sina.com.cn/s/blog_8604ca230100vro9.htmlspa
如今要動態的變化分頁查詢的條件,好比pageNow 這個變量表示的是當前是第幾頁,
oracle分頁有通用寫法,假設一頁5行
select * from (
select t.*,rownum from (
select * from table1 where condition order by column) t )
where rownum>(pangeNow-1)*5 and rownum<=(pageNow)*5
若是基礎查詢不須要排序,能夠省掉一層嵌套
select * from (
select t.*,rownum from table1 t where condition )
where rownum>(pangeNow-1)*5 and rownum<=(pageNow)*5
Oracle:方法一:SELECT * FROM(SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A
WHERE ROWNUM <= 40)WHERE RN = 21;方法二:SELECT * FROM(SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A)WHERE RN between 21 and 40
公認第二種方法效率沒有第一種高。緣由是第二種要把子查詢執行完,而第一種方法子查詢執行到Rownum=40後就結束了
二、關聯查詢:
select s5.Ma003,s5.Ma004,s5.Ma005,u1.Ma008 from S005 s5,U001 u1 where s5.ma005=u1.ma001
對上條語句優化:
select s5.MA001, s5.MA002, s5.MA003, s5.MA004, s5.MA005, s5.MA006, s5.MA007,u1.Ma008
from S005 s5 left join U001 u1 on s5.ma005=u1.ma001
where s5.MA002 ='5973a2ad-64ad-4fc8-b94e-440d0c67e0bc' and s5.Ma005='be2666a4-85d5-4d70-86c2-b854f457f3cd' order by s5.MA004 desc
多表關聯:
select v.areaid ,v.corpid,a.areaname,c.corpname from TMONITORAREA_CORP v
left join tmonitorarea a on a.areaid=v.areaid
left join tmonitorcorp c on c.corpid=v.corpid
三、時間段區間查詢:
select *
from s002 where ma004 between to_date('2014-7-1 07:00:00','yyyy-mm-dd hh:mi:ss')
and to_date('2014-7-16 07:00:00','yyyy-mm-dd hh:mi:ss')
4.經常使用sql語句及案例(oracle)
http://www.cnblogs.com/wishyouhappy/p/3700683.html
ORACLE 博客文章目錄(2014-07-19更新)
http://www.cnblogs.com/kerrycode/p/3256266.html
5.子查詢與關聯查詢的區別?
http://www.cnblogs.com/hoojo/archive/2011/07/19/2110862.html 關注此人的博客,寫的挺不錯的!
七、Case When Then多條件判斷
select count(case when (status = '未鏈接' or status = '故障' or status = '傳感器故障' or status = '通信超時' or status = '網絡故障') then 1 else null end) faultTotal from v_trealtimedatainfo_p_m_c
count(case when status = '正常' then 1 else null end) normalTotal
語法:
CASE
WHEN 條件1 THEN 結果1
WHEN 條件2 THEN 結果2
WHEN 條件3 THEN 結果3
WHEN 條件4 THEN 結果4
.........
WHEN 條件N THEN 結果N
ELSE 結果X
END
Case具備兩種格式。簡單Case函數和Case搜索函數。
--簡單Case函數
CASE sex
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE '其餘' END
--Case搜索函數
CASE WHEN sex = '1' THEN '男'
WHEN sex = '2' THEN '女'
ELSE '其餘' END
例如:
SELECTid, name, cj, (CASE WHEN cj < 60 THEN '不及格' WHEN cj BETWEEN 60 AND 90 THEN '良好' WHEN cj > 90 THEN '優秀' END) AS 狀態FROMstud