1、分页
不管手机端需不需要分页,我这个接口都通用,并且要查询效率高
sql语句怎么写?pageNamber怎么用?数据怎么返回?分页策略一:先查询出全部数据然后再分页。getData?page=1
mSublist=list.sublist(start,end);mSublist=list.sublist(30*(page-1)+1,30*page);如果要查询全部 只需要start=end=0
然后返回这个mSublist 这样的话需要写一个分页的工具类。如果数据量特别大,全部都查出来再分页效率很低。分页策略二:
select * from table where row_num between 30*(pageNamber-1)+1 and 30*pageNambe;
pn=1 1-30
pn=2 31-60SELECT * FROM(SELECT T.*,ROWNUM RN FROM U001 T) WHERE RN >=30*(pageNamber-1)+1 AND RN <= 30*pageNambe 这也是可以的
果没有分页需求,这个不通用啊。写两个sql语句,如果pageNumber<1只写全部查询,否则按照第几页显示
orcale高效分页:
现在要动态的变化分页查询的条件,比如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)*5Oracle:方法一:SELECT * FROM(SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) AWHERE ROWNUM <= 40)WHERE RN = 21;方法二:SELECT * FROM(SELECT A.*, ROWNUM RNFROM (SELECT * FROM TABLE_NAME) A)WHERE RN between 21 and 40公认第二种方法效率没有第一种高。原因是第二种要把子查询执行完,而第一种方法子查询执行到Rownum=40后就结束了2、关联查询:
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.areaidleft join tmonitorcorp c on c.corpid=v.corpid
3、时间段区间查询:
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)
ORACLE 博客文章目录(2014-07-19更新)
5.子查询与关联查询的区别?
6.
关注此人的博客,写的挺不错的!
7、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 结果XENDCase具有两种格式。简单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