Mardoxx Posted July 29, 2009 Share Posted July 29, 2009 $sub_sql = "SELECT id, name_person, name_data FROM names WHERE name_id IN($ids)"; when $ids = "1,2,3"; it outputs 1 Adam - data1 2 Bill - data2 3 Charlie - data3 when $ids = "3,2,1"; it outputs 1 Adam - data1 2 Bill - data2 3 Charlie - data3 instead of the expected reverse order... It seems that sql is auto ordering it... any ideas what's up or how to stop it? thanks //edit while(list($id,$name,$data) = mysql_fetch_array($result)) { echo "$id - $name - $data"; } Quote Link to comment https://forums.phpfreaks.com/topic/167996-solved-sql-query-not-working-as-expected-it-auto-orders-the-results/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 29, 2009 Share Posted July 29, 2009 IN() just matching things, it does not determine the ORDER BY used in a query. You would need to use an ORDER BY term to get a specific order. Quote Link to comment https://forums.phpfreaks.com/topic/167996-solved-sql-query-not-working-as-expected-it-auto-orders-the-results/#findComment-886072 Share on other sites More sharing options...
Mardoxx Posted July 29, 2009 Author Share Posted July 29, 2009 but surely if it matches 3 first, then 2 then 1 it should output in that order, no? Quote Link to comment https://forums.phpfreaks.com/topic/167996-solved-sql-query-not-working-as-expected-it-auto-orders-the-results/#findComment-886077 Share on other sites More sharing options...
PFMaBiSmAd Posted July 29, 2009 Share Posted July 29, 2009 Queries don't work like that. Queries produce a set of rows that match the conditions you specify. Without an ORDER BY term the rows in the result set are returned in the order in which they exist in the table, which can be anything. Quote Link to comment https://forums.phpfreaks.com/topic/167996-solved-sql-query-not-working-as-expected-it-auto-orders-the-results/#findComment-886084 Share on other sites More sharing options...
Mardoxx Posted July 29, 2009 Author Share Posted July 29, 2009 ahhh righty so how can I order them by $ids? //edit found it ORDER BY FIND_IN_SET(id, '$ids') Quote Link to comment https://forums.phpfreaks.com/topic/167996-solved-sql-query-not-working-as-expected-it-auto-orders-the-results/#findComment-886087 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.