Jump to content

Nesting results of one query in another


havencruise
 Share

Recommended Posts

Hi all,

I'm new to php and I was trying to send the results of one query to another query. I need the results of both queries.

Here are the queries

$cui1 = mysql_query("SELECT DISTINCT CUI FROM MRSTY WHERE STY='ABC' ");

$cui2 = mysql_query("SELECT DISTINCT CUI FROM MRSTY WHERE STY='XYZ' ");

 

I want to know if this is possible

$cuis = mysql_query("SELECT CUI1,CUI2,REL FROM MRREL WHERE CUI1 IN $cui1 OR CUI1 IN $cui2 ");

 

Thanks.

Link to comment
Share on other sites

The first two queries get a list of all CUIs - which are basically IDs of a certain string STY like xyz or abc. CUI is not the primary key in the MRSTY table. I need the results of this table.

The third query is an "IN" subquery which basically sends the CUIs obtained in the first two queries as conditions. It is more like saying:

Select name from employee where cafeteria_id in 
   (select cafeteria_id from cafe where branch = "new york") 
or cafeteria_id in (select cafeteria_id from cafe where branch = "london")

What I'm trying to do is perform the first two queries and store the results in a variable like:

$cui1 = select cafeteria_id from cafe where branch = "london"
$cui2= select cafeteria_id from cafe where branch = "new york"

and then send the first two variables into the third query:

Select name from employee where cafeteria_id in $cui1 or cafeteria_id in $cui2

Link to comment
Share on other sites

Thank you, I already have the CUIs in variables. My question is how do I send these values from the variables to the query.

 

The code below does not work. What can I do to make it work?

$cuis = mysql_query("SELECT CUI1,CUI2,REL FROM MRREL WHERE CUI1 IN $cui1 OR CUI1 IN $cui2 ");

Link to comment
Share on other sites

Thank you, I already have the CUIs in variables. My question is how do I send these values from the variables to the query.

 

You don't. There is no reason to run multiple queries. And you don't have the CUI's in a variable.

 

$cuis = mysql_query("SELECT CUI1,CUI2,REL FROM MRREL WHERE CUI1 IN $cui1 OR CUI1 IN $cui2 ");

In that code $cuis is a resource identifier to a MySQL result set. You would have to loop through that result set to get the values to use in a subsequent query. Why would you not want to run just one queery anyway?

Link to comment
Share on other sites

You don't. There is no reason to run multiple queries. And you don't have the CUI's in a variable.

In that code $cuis is a resource identifier to a MySQL result set. You would have to loop through that result set to get the values to use in a subsequent query.

Thank you.. That sounds exactly like what I need to learn to do.

Why would you not want to run just one queery anyway?

 

The first two queries which output into $cui1 and $cui2 take about an hour to select about 70 values. I do not want to execute that in another nested query. I would rather pass the result set as a parameter to the third query.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.