473,699 Members | 2,812 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 4403
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
1838
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
8140
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
1647
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
1430
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
2588
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
4088
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
9025
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
1626
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
2591
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
8686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
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
9173
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
9033
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
8911
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
7748
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...
1
6533
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5872
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2345
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.