oracle中union无法做分页查询,需要用rownum来做分页。
如emp表中有如下数据:
现在要对数据进行编号,分页,可用如下语句:
select rownum,emp.* from emp;
查询结果:
就在你查询的sql 外面
套上
select t2.* from (
select t1.* ,rownum rn from ( 你自己的sql) t1 where rownum< 10) t2
where rn >0;
这里假设你要取的是 第1到 第10条 ,由于sql执行时,是从最内层的 那个查询开始的,所以首先取 小于上限10的值,你懂得,这样可以缩小第二次查询时的数据,提高查询性能。然后在过滤下限就行了。
具体的上限值与下限值,根据你的需要来进行赋值
select * from (
select ct.*,rownum rn from (
select SUPP.adm_id id,SUPP.shop_name name,SUPP.img, 1 as pg
from CT_admin_extend SUPP
where SUPP.shop_name like '%锦江之星%'
union all
select PRO.id,PRO.name,PRO.img,2 as pg
from CT_supp_product PRO
where PRO.name like'%锦江之星%'
union all
select HD.id,HD.name,HD.img,3 as pg
from CT_supp_coupon HD
where HD.name like'%锦江之星%'
order by id) ct)
where rn between 1 and 20
你要这个?
select * from
(select A.*,rownum r from (
select SUPP.adm_id id, SUPP.shop_name name, SUPP.img, 1
from CT_admin_extend SUPP
where SUPP.shop_name like '%锦江之星%'
union all
select PRO.id, PRO.name, PRO.img, 2
from CT_supp_product PRO
where PRO.name like '%锦江之星%'
union all
select HD.id, HD.name, HD.img, 3
from CT_supp_coupon HD
where HD.name like '%锦江之星%') A where rownum <=20)
where r>=10;
这个是用oracle数据库分页显示10~20的数据,当然也可以写成下面的,不过效率没有上面的高。
select * from
(select A.*,rownum r from (
select SUPP.adm_id id, SUPP.shop_name name, SUPP.img, 1
from CT_admin_extend SUPP
where SUPP.shop_name like '%锦江之星%'
union all
select PRO.id, PRO.name, PRO.img, 2
from CT_supp_product PRO
where PRO.name like '%锦江之星%'
union all
select HD.id, HD.name, HD.img, 3
from CT_supp_coupon HD
where HD.name like '%锦江之星%') A )
where r between 10 and 20;