473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

combining two queries, but leave them separate (mySQL)

Hi,

I know this might sound strange but i think(/hope)
it's quite simple:

I'm running 2 queries in a mysql DB, first one returns 20 results.

Now how can i echo results from the second query in the 1st query's
result,
like:

do{

echo 'foo'.$second_result['thingy'];

}while($first_result = $first_query);

I hope this makes sense...

Greetings frizzle

Jul 17 '05 #1
7 2779
* please, anyone ? *

Jul 17 '05 #2
NC
frizzle wrote:

I know this might sound strange but i think(/hope)
it's quite simple:

I'm running 2 queries in a mysql DB, first one returns
20 results.

Now how can i echo results from the second query in the
1st query's result


What exactly are you trying to do? You can output all
results of the second query next to each result of the
first query, or you can pair records returned by the
first query with those returned by the second query.
Which are you trying to accomplish? Or is it something
else?

Also, you can attempt to combine the two queries into
one using JOIN or UNION.

Cheers,
NC

Jul 17 '05 #3
I need to pair the queries, but i want to leave them separate (as i
said). I've told my problem earlier on
http://groups-beta.google.com/group/...141a9a168bbcd1

But they told me to redesign my tables. I know that this can be solved
differently, without redesigning my tables and combining the queries...

I hope this clears things out a little bit more...

Thanks in advance...

Jul 17 '05 #4
NC
frizzle wrote:

I need to pair the queries, but i want to leave them separate (as i
said). I've told my problem earlier on
http://groups-beta.google.com/group/...thread/thread/ 6cf997d613e2a595/ce141a9a168bbcd1#ce141a9a168bbcd1

But they told me to redesign my tables.
It wasn't "they". It was I. :)
I know that this can be solved differently, without redesigning my tables and combining the queries...


It can, but performance is going to suck. What you are looking for is
this:

$result1 = mysql_query('SELECT id, name, buddies_id FROM employees');
while ($record1 = mysql_fetch_assoc($result1)) {
echo $record1['name'], "'s buddies are: \r\n";
$result2 = mysql_query('SELECT id, name FROM employees WHERE id IN ('
..
str_replace('-', ',', $record1['buddies_id']) . ')');
while ($record2 = mysql_fetch_assoc($result2)) {
echo $record2['name'], "\r\n";
}
}

What you don't seem to realize is that a query takes a certain time to
prepare for execution. Assuming that time is 0.1 seconds (a not
particularly scientific rule of thumb), by asking MySQL to do 21
queries where one would suffice, you are asking for 2 extra seconds
of preparation time, even before MuSQL actually executes any of the
queries. If you redesigned your tables, the seemingly complicated
query would be resolved in way under one second.

Cheers,
NC

Jul 17 '05 #5
lol it was you, could've checked... ;)

Anyway, i understand the method you supplied this time, and i know
it's going to take a lot of time. That's why a want to avoid this
situation.

My current situation is sort of like this:
SELECT id, name, function, buddies FROM employees LIMIT 20

do{

$output .= $first_query['name'];
$output .= " - ".$first_query['function']." - ";
$output .= "<br>&nbsp&nbsp&nbsp";
$output .= "buddies:"
$output .= ; //place for buddies names

}while(employees are returned);

returned buddies form 1st query are put in an unique, sorted array, and
then the second query is runned:
SELECT id, name FORM emplyees WHERE id IN($array)

Then i need to mix up the second query's result mixed with $output..>

Hope this clears up the situation a little bit. If not, please ask,
because i'd really like this to work. :)

Thanks anyway for taking the effort again to have a look at my
problems..

Greetings!

Jul 17 '05 #6
NC
frizzle wrote:

Anyway, i understand the method you supplied this time, and i know
it's going to take a lot of time. That's why a want to avoid this
situation.

My current situation is sort of like this:
SELECT id, name, function, buddies FROM employees LIMIT 20

do{

$output .= $first_query['name'];
$output .= " - ".$first_query['function']." - ";
$output .= "<br>&nbsp&nbsp&nbsp";
$output .= "buddies:"
$output .= ; //place for buddies names

}while(employees are returned);

returned buddies form 1st query are put in an unique, sorted array,
and then the second query is runned:
SELECT id, name FORM emplyees WHERE id IN($array)


OK, this should solve your problem then:

$output = array();
$ids = array();
$result1 = mysql_query('SELECT id, name, buddies_id FROM employees');
while ($record1 = mysql_fetch_assoc($result1)) {
$i = $record1['id'];
$buddies = explode('-', $record1['buddies_id']);
foreach ($buddies as $buddy) {
if (!in_array($buddy, $ids)) {
$ids[] = $buddy;
}
}
$output[$i] = array('name' => $record1['name'],
'buddies' => $buddies);
}
$buddies = array();
$result2 = mysql_query('SELECT id, name FROM employees WHERE id IN (' .

implode(',', $ids) . ')');
while ($record2 = mysql_fetch_assoc($result2)) {
$i = $record2['id'];
$buddies[$i] = $record2['name'];
}
foreach ($output as $employee) {
echo $emloyee['name'], "'s buddies are:\r\n";
foreach ($employee['buddies'] as $i) {
echo $buddies[$i], "\r\n";
}
}

Cheers,
NC

Jul 17 '05 #7
Wow, i fixed it! (Together with you of course :) )

Anyway, i didn't use your answer, but you gave me an idea to come to a
solution that even i was capable of understanding:

Now i'm running a str_replace for each buddy_id, and it works great! :)
Thanks for helping me out, i'm really glad i got this working!!!
:)

(Do you think str_replace is a heavy function to perform..?)

Jul 17 '05 #8

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

Similar topics

0
by: Skip Montanaro | last post by:
Consider the output of these two explain statements: mysql> explain select count(*) from cities,addresses,venues,events where cities.latitude <= 30.2741903768 and cities.latitude >=...
0
by: SimonC | last post by:
I'm looking to do something similar to a feature found on Ticketmaster.com, where you select your seats at a venue, and then you have two minutes in which to take or leave them. QUESTION 1a....
0
by: BaKMaN | last post by:
I am using ASP/VBScript to produce a XML file based on a AccesDB query (using adPersistXML) and include a XSL, by itself this works nicely. Is it possible to combine 2 separate queries into one...
9
by: mfyahya | last post by:
Hi, I'm new to databases :) I need help speeding up select queries on my data which are currently taking 4-5 seconds. I set up a single large table of coordinates data with an index on the fields...
2
by: SomeDude | last post by:
Lo group, I am wondering if there is a way of combining two SELECT statements into a single query. Here's the obligatory example to clarify things: SELECT id WHERE name=mike SELECT bills...
1
by: zek2005 | last post by:
Hi!!!! I´m stuck with a problem I have to make a table with the information of three different queries. The table has 4 columns x n rows. The first and second columns must be filled with the...
7
by: Daz | last post by:
Hi. I am trying to select data from two separate MySQL tables, where I cannot use join, but when I put the two select queries into a single query, I get an error telling me to check my syntax. Both...
5
by: Tristan Miller | last post by:
Greetings. Is it possible using HTML and CSS to represent a combining diacritical mark in a different style from the letter it modifies? For example, say I want to render Å‘ (Latin small letter...
6
by: autoalert | last post by:
HI I am running mysql 5.0.51a and have a table with the following structure ID, Date, startTime, stopTime, Name I want to find the last stopTime from yesterday for each name and the first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.