0

I want to use pagination in php but the problem is there is union in query like:

select * from table1 UNION select * from table 2

How can i achieve pagination in this case

1 Answer 1

1

if you speak of LIMIT .. OFFSET then usage is same. eg:

t=# select oid,right(datname,3) from pg_database
union all
select oid,right(datname,3) from pg_database
order by oid;
    oid    | right
-----------+-------
         1 | te1
         1 | te1
     13289 | te0
     13289 | te0
     13294 | res
     13294 | res
     17635 | ats
     17635 | ats
   1175099 | t
   1175099 | t
  35836773 | pme
  35836773 | pme
 129444516 | s
 129444516 | s
 185505699 | foo
 185505699 | foo
 281585738 | ump
 281585738 | ump
(18 rows)

Time: 0.404 ms
t=# select oid,right(datname,3) from pg_database
union all
select oid,right(datname,3) from pg_database
order by oid
limit 5 offset 3;
  oid  | right
-------+-------
 13289 | te0
 13294 | res
 13294 | res
 17635 | ats
 17635 | ats
(5 rows)

Time: 0.437 ms

Mind that I use UNION ALL, not UNION, so postgres would not hide duplicated rows

Sign up to request clarification or add additional context in comments.

2 Comments

ok, if i have large no of records coming from union then does it has performance issue ?
might be. number of rows is one of variables

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.