views:

69

answers:

3

Hey all, I am wondering what the best way to do the following is:

Query up to 5 tables (depends on user input) pull back the results and return a single array. Lets say I query table A and have the results stored as a result handle (return from the pg_query() fn), shall I go ahead and convert those to an array, and then continue appending the results from the subsequent queries to that? If so can I get away with simply using the array_merge() function?

+1  A: 

Sounds like you want a UNION.

chaos
I would rather query each table (actually a view) independently and then join the results using PHP, hence my thought of using the array_merge() fn.
Nicholas Kreidberg
Okay. Then yes, `array_merge()` is what you want.
chaos
A: 

use a union:

SELECT col1, col2
  FROM a
 UNION
SELECT col1, col2
  FROM b
knittl
A: 

Using a UNION might be the answer only if your results are going to be low. UNION's are a little taxing on the DB server and some DB's have a hard time optimizing them. By using a Store Procedure or Making 5 Queries then doing an array merge, will allow the DB server to cache some results and improve DB performance.

If the result set is going to be low and there are not going to be much calls, then using a UNION should be fine.

MANCHUCK
The result set will be in the 10s of thousands.
Nicholas Kreidberg