Emmash Posted October 22, 2008 Share Posted October 22, 2008 Hi, I have a query like : SELECT no_document,titre_document FROM document WHERE no_document=3 OR no_document=1 OR no_document=2 and I have a loop like : while($ligne=mysql_fetch_array($requete)){ ... } When I display the results in the loop, I get document 1, document 2 and document 3 displayed instead of document 3, document 1 and document 2 like in my where clause. Is there a way to keep the order like the order in the WHERE clause because my results always seems to re-order automatically by id... Thanks to help me!!! Marie-Hélène Quote Link to comment https://forums.phpfreaks.com/topic/129582-solved-problems-with-queries/ Share on other sites More sharing options...
beansandsausages Posted October 22, 2008 Share Posted October 22, 2008 ORDER BY DESC or ORDER BY ASC Quote Link to comment https://forums.phpfreaks.com/topic/129582-solved-problems-with-queries/#findComment-671781 Share on other sites More sharing options...
trq Posted October 22, 2008 Share Posted October 22, 2008 There isn't a way (that I'm aware of) to order by the order of your WHERE clause. On a side note, your query could be made simpler. SELECT no_document,titre_document FROM document WHERE no_document IN(3,1,2); Quote Link to comment https://forums.phpfreaks.com/topic/129582-solved-problems-with-queries/#findComment-671785 Share on other sites More sharing options...
Emmash Posted October 22, 2008 Author Share Posted October 22, 2008 There isn't a way (that I'm aware of) to order by the order of your WHERE clause. On a side note, your query could be made simpler. SELECT no_document,titre_document FROM document WHERE no_document IN(3,1,2); Same problem... the documents are not ordered as the value in the "IN" clause... I get again document 1, 2, 3 instead of 3,1,2 Quote Link to comment https://forums.phpfreaks.com/topic/129582-solved-problems-with-queries/#findComment-671806 Share on other sites More sharing options...
beansandsausages Posted October 22, 2008 Share Posted October 22, 2008 Same problem... the documents are not ordered as the value in the "IN" clause... Thore told you you can't do it: There isn't a way (that I'm aware of) to order by the order of your WHERE clause. On a side note, your query could be made simpler. SELECT no_document,titre_document FROM document WHERE no_document IN(3,1,2); Looks like you may need to re think it or do three sperate querys. Quote Link to comment https://forums.phpfreaks.com/topic/129582-solved-problems-with-queries/#findComment-671827 Share on other sites More sharing options...
PFMaBiSmAd Posted October 22, 2008 Share Posted October 22, 2008 Please read this - http://www.eklekt.com/tutorials/mysql Quote Link to comment https://forums.phpfreaks.com/topic/129582-solved-problems-with-queries/#findComment-671851 Share on other sites More sharing options...
Emmash Posted October 22, 2008 Author Share Posted October 22, 2008 I found the solution by reading in MYSQL Documentation.... Here is the solution : SELECT no_document,titre_document FROM document WHERE no_document=3 OR no_document=1 OR no_document=2 ORDER BY FIELD(no_document,'3','1','2') Quote Link to comment https://forums.phpfreaks.com/topic/129582-solved-problems-with-queries/#findComment-671868 Share on other sites More sharing options...
trq Posted October 22, 2008 Share Posted October 22, 2008 Well done. I'm not sure when that will come in handy but it might. Quote Link to comment https://forums.phpfreaks.com/topic/129582-solved-problems-with-queries/#findComment-671890 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.