473,748 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best Way to Combine Results from 2 mySQL Searches, into One Sorted List?

I have to search 2 mySQL tables, and show the user a single sorted list
that contains all the results from both mySQL queries.

My question is, how do you get all the resulting items from both
databases into one single, sorted list, to present to the user?

I guess one way would be, to use PHP to copy all the results from each
database, into a PHP array, and then sort it. Is that how this kind of
thing is usually done?

Thanks in advance to all for any info.
Aug 20 '07 #1
3 4405
Rik
On Mon, 20 Aug 2007 23:32:46 +0200, Vik Rubenfeld
<vi**@mindsprin g.com.invalidwr ote:
I have to search 2 mySQL tables, and show the user a single sorted list
that contains all the results from both mySQL queries.
I'm curious about the data/tables, it might be possible to do all this on
a MySQL level instead of PHP, which would usually be better.
My question is, how do you get all the resulting items from both
databases into one single, sorted list, to present to the user?
$list = array();

$r1 = mysql_query("En ter query one here");
while($row = mysql_fetch_ass oc($r1)) $list[] = $row;

$r2 = mysql_query("En ter query two here");
while($row = mysql_fetch_ass oc($r2)) $list[] = $row;

//untested....:
$fieldname = 'foo'; //<- enter the fieldname to sort by
usort($list, create_function ('$a,b',"if(\$a['{$fieldname}'] ==
\$b['{$fieldname}']) return 0; return \$a['{$fieldname}']
\$b['{$fieldname}'] ? 1 : -1;");
I guess one way would be, to use PHP to copy all the results from each
database, into a PHP array, and then sort it. Is that how this kind of
thing is usually done?
The database software is optimised for this kind of stuff, so if it can be
done there that would be highly preferable.
--
Rik Wasmus
Aug 20 '07 #2
On Aug 20, 5:32 pm, Vik Rubenfeld <v...@mindsprin g.com.invalidwr ote:
I have to search 2 mySQL tables, and show the user a single sorted list
that contains all the results from both mySQL queries.

My question is, how do you get all the resulting items from both
databases into one single, sorted list, to present to the user?

I guess one way would be, to use PHP to copy all the results from each
database, into a PHP array, and then sort it. Is that how this kind of
thing is usually done?

Thanks in advance to all for any info.
Use union:

<http://dev.mysql.com/doc/refman/5.0/en/union.html>

Aug 21 '07 #3
Awesome. Thanks very much, guys!
Aug 21 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1839
by: tina.boroff | last post by:
I didn't know what to use as a title for this. Here is the scenario: I have a op.reqedit.php page. This page a form where you input data. In this page I have split a MySQL field. It is called MachineTypeOfWork and is split into Replace, Adjusted, REbuild, Inspected and Repair. These all become check boxes for the user. The form submits to op.reqadd.php. In this page I first have a hidden box where I combine all of the above into...
21
8146
by: Imran | last post by:
I have a vector of integers, such as and I want to find out the number which occurs most frequently.what is the quick method. My array size is huge. what I am doing is 1. find out the maximum value N 2. loop through 1...N 3. count # times each occurred
21
1651
by: John Salerno | last post by:
If I want to make a list of four items, e.g. L = , and then figure out if a certain element precedes another element, what would be the best way to do that? Looking at the built-in list functions, I thought I could do something like: if L.index('A') < L.index('D'): # do some stuff But I didn't know if maybe there was a preferred method for this type of
5
1433
by: himilecyclist | last post by:
I have a query screen where the user has an option to search by name fields in the database. There are first, middle and last name fields and the results returned should be sorted last, first, middle. Here is the WHERE part of my query: WHERE CONCAT(Last, First, Middle) >= CONCAT('$_POST', '$_POST', '$_POST')"; There is an ORDER BY and LIMIT 200 at the end of this and the results are loaded into a list box.
8
2596
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array; m_ResultArray.Add ( aFoundObject);
6
4092
by: pj | last post by:
Hi, I 'm currently writing a program that performs transliteration (i.e., converts greek text written using the english alphabet to "pure" greek text using the greek alphabet) as part of my thesis. I have decided to add the capability to convert words using some sort of lookup algorithm as a sidekick to the "normal" conversion algorithm, and here it starts getting interesting. I want to find an algorithm that satisfies these...
9
9028
by: Aaron Watters | last post by:
....is to forget they are sorted??? While trying to optimize some NUCULAR libraries I discovered that the best way to merge 2 sorted lists together into a new sorted list is to just append them and re-sort. The following test case demonstrates this. It can be criticized in many ways: it only tests lists of the same size, it only uses "hashed" data, etcetera... Still, my testing shows "resort everything" is consistently
1
1628
by: russot00 | last post by:
I have 3 drop down menus that are used in a search to locate restaurants in a db. All of the drop down menus function, a search can be submitted with any combination of drop downs and the results are sorted by restaurant name. The problem occurs when you do not select anything from a drop down ("All" is submitted for all drop downs) and submit the search, the following error occurs: "Can't connect because You have an error in your SQL syntax;...
1
2593
by: bobby44 | last post by:
Hi, I hope someone can help with a query I'm stuck on. I think it will involve inner join but I can't get my head around it. I have two tables that relate to venues fr weddings: -- listingDBElements -- ID field_name field_value listing_id user_id 23 county Yorkshire 34 56
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9541
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4602
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.