Grant Giddens <[EMAIL PROTECTED]> wrote on 03/28/2005 12:43:09 PM: > Hi, > > If I do a query like: > > SELECT prodname, price FROM prod_table WHERE sku in > ($sku1, $sku2, $sku3, $sku4) > > > Will my results always be ordered in $sku1, $sku2, > $sku3, $sku4 order? I can't really do a ORDER BY > prodname or price here. > > I just want to make sure that that this type of query > will always return the results in $sku1, $sku2, $sku3, > $sku4 order. > > Thanks, > Grant > > > > __________________________________ > Do you Yahoo!? > Yahoo! Small Business - Try our new resources site! > http://smallbusiness.yahoo.com/resources/ >
No, without a GROUP BY (which, unless told to do otherwise, sorts its results) or an ORDER BY clause in your query there is no guarantee that queries will return rows in the same order for each execution. If you need to have results in a particular order, you must use an ORDER BY clause to guarantee that order. You don't have to sort by column names, you can sort on the results of functions too (created in the same pattern as your sample query): ORDER BY IF(sku=$sku1, 1, if(sku=$sku2, 2, if(sku=$sku3,3,4))) Before I get flamed... Yes, if the query's results were cached then you should get the same rows in the same order for the same query. However, there is no guarantee that your query will return (or cache) the rows sorted by the sequence of the terms your IN clause. In fact I doubt that you would get many results that happened (randomly) to be in the order of the values as you specify them in your IN clause. To guarantee a record sequence, you must use an ORDER BY. Sorry! Shawn Green Database Administrator Unimin Corporation - Spruce Pine