Merging MySQL query results

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
matrix64
Forum Newbie
Posts: 2
Joined: Mon Jun 02, 2008 5:42 pm

Merging MySQL query results

Post by matrix64 »

Hi,

I have data horizontally partitioned over several MySQL servers. After I connect and run a query on each server, I'd like to merge the results they give me and use that data in a loop to fill a table with data, reverse sorted by a PRIMARY KEY. I presume this would be done with arrays.

I'm still quite new to PHP and I'd like some help solving this problem.

Thank you.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Merging MySQL query results

Post by Benjamin »

Code: Select all

 
$result = array_merge($array1, $array2, $array3);
 
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Merging MySQL query results

Post by Eran »

You might be better off performing a UNION to combine all the different datasets into one at the database level. You can then apply ordering and grouping clauses on the entire dataset.
matrix64
Forum Newbie
Posts: 2
Joined: Mon Jun 02, 2008 5:42 pm

Re: Merging MySQL query results

Post by matrix64 »

astions wrote:

Code: Select all

 
$result = array_merge($array1, $array2, $array3);
 
Indeed, but:
1) How do I merge the entire result set and not just the first line?
2) How do I get this new array to do the same as mysqli_fetch_row in a loop?
pytrin wrote:You might be better off performing a UNION to combine all the different datasets into one at the database level. You can then apply ordering and grouping clauses on the entire dataset.
The datasets are on different servers. Federated tables are not an option.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Merging MySQL query results

Post by Benjamin »

I recommended array_merge assuming you already have all 3 complete result sets stored in arrays. You can use $record = array_shift($array); to pull a record off the array stack or you can loop through them with foreach(). Have a look at the array functions for details. Without knowing more that's about all I can tell you.

http://us2.php.net/manual/en/ref.array.php
Post Reply