Page 1 of 1

Using queried results in a seperate query Order by

Posted: Thu Mar 22, 2012 9:44 am
by rtblizz
I am taking results ($result2) that I get from a one query and formatting them in a way to be inserted in another query. This works fine using the code below

Code: Select all


//take results and customize them for query value insert

while ($row = mysql_fetch_array($result2, MYSQL_ASSOC))
    {
        $strquery .= " ".$row["zipcode"].", ";
    }

//take list and stop the , at end of query insert

    $strquery = rtrim($strquery, ', ');

doing this above it gives me these result: 21146, 21032, 21123, 21144

then I insert them here and it works great.

Code: Select all


$query = "select * from Table where ZipCode IN (".$strquery.") order by Value";

that's where my question comes in I would like to order by the results and Value??

My results currently display zip codes in ascending order like this. (21032, 21123, 21144, 21146) low to high

How can I get the query to display results in the results order above. (21146, 21032, 21123, 21144)

My thought was something like this but it breaks when I use it..

Code: Select all


$query = "select * from Table where ZipCode IN (".$strquery.") order by Value, ZipCode [(".$strquery.")]";

any help would be appreciated :)

Re: Using queried results in a seperate query Order by

Posted: Fri Mar 23, 2012 4:06 pm
by mecha_godzilla
Hi,

I don't think you can specify the exact order of returned results using specific IDs because (in theory) you don't know what results are going to be returned. As far as I can tell, ORDER BY is used with one or more keys and you then just have the option of specifying whether the results are ASC or DESC. Looking at your code, I can't tell what criteria is being used to order your SELECTed values but you can always sort the array afterwards in your script if needed.

HTH,

Mecha Godzilla