views:

73

answers:

5

In some languages (ColdFusion comes to mind), you can run a query on the result set from a previous query. Is it possible to do something like that in php (with MySQL as the database)?

I sort of want to do:

$rs1 = do_query( "SELECT * FROM animals WHERE type = 'fish'" );
$rs2 = do_query( "SELECT * FROM rs1 WHERE name = 'trout'" );
+2  A: 

Well, you may want to do this without touching the db:

while($t = mysql_fetch_array($rs1)){
    if($t[name] == 'trout'){ echo 'This is the one we\'re looking for!'; break;}
Robus
+1  A: 

In PHP, it would be terribly inefficient. You would have to loop through each row and check that its name was trout. However, is there any reason you can't do

SELECT * FROM `animals` WHERE `type` = 'fish' AND `name` = 'trout'

in SQL? It would be much, much faster.

waiwai933
I don't think you understand the purpose of a query of queries.
animuson
Wouldnt searching for it in PHP's memory be faster than hammering on a large DB again? I might be wrong though.
Robus
@Robus: Probably. If you get 100 out of 1,000 results with the initial fish query, it would be much better to search in PHP rather than sifting through 1,000 results again with an additional query.
animuson
@Robus SQL is optimized for interacting with data in databases, so probably not, taking a properly indexed DB into account.
waiwai933
@waiwai933 I guess that it mostly depends on luck. I mean looking at the function I wrote in my answer, it might get the desired row on the first loop - making it much faster than the database. But then again it might be the last one, in which case the DB will most likely be faster.
Robus
The database will only be faster for very small result sets. If the database has 6,000,000 entries and only 100 of those entries belong to 'username', then running two queries on that is horrible. I don't see running an extra MySQL query ever being a better alternative.
animuson
A: 

Use the AND keyword?

"SELECT * FROM animals WHERE type = 'fish' and name='trout'"

Also, you can use LINQ for php http://phplinq.codeplex.com/

Ilya Biryukov
What if you want the results for the fish query and then the results plus the trout? Then you have to run two separate queries...
animuson
+1  A: 

There is no MySQL function like this for PHP, however there is a more advanced substitute for it.

Edit: For those of you who don't know what a query of queries is, it's exactly this and there's a purpose some people do it like this. Using an AND operator is *NOT* the same thing! If I want results where username='animuson' for one part of my script and then want all the results out of that query where status='1', it is not logical for me to run another query using an AND operator, it is much more logical to loop through the previous results in PHP. Stop upvoting things without reading the comments on why they weren't upvoted in the first place, that's just lazy. If you don't have a clue what's being talked about, you shouldn't be upvoting or downvoting in the first place.

animuson
A: 

You can also do something like

select morestuff from (select stuff from table where a = b ) where c = d;
seamus
This is just a more complicated and much slower way of using an AND operator.
animuson