Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem with ORDER BY

Newbie
 
Join Date: Oct 2008
Posts: 10
#1: Oct 22 '08
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

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Oct 22 '08

re: Problem with ORDER BY


That's what the ORDER BY clause is used for. You did not include it in your query.
Newbie
 
Join Date: Oct 2008
Posts: 10
#3: Oct 22 '08

re: Problem with ORDER BY


I don't want to order my query with one of the field in the table... that's the problem!!!

I want to order my query depending on what the user choose.

So, the first no document has to be the no document chose by the user and the others has to be alphabetically ordered.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Oct 22 '08

re: Problem with ORDER BY


Quote:

Originally Posted by Emmash

I don't want to order my query with one of the field in the table... that's the problem!!!

I want to order my query depending on what the user choose.

So, the first no document has to be the no document chose by the user and the others has to be alphabetically ordered.

I understand the problem now.
Try something like
Expand|Select|Wrap|Line Numbers
  1. order by (case columnName when yourPreferedValue then 1 else 2 end)
Basically you are ordering by a particular column (columnName) but rows with a value of yourPreferedValue will be returned first.
Newbie
 
Join Date: Oct 2008
Posts: 10
#5: Oct 22 '08

re: Problem with ORDER BY


[PHP]SELECT no_document,titre_document FROM document WHERE no_document=2 OR no_document=3 OR no_document=1 ORDER BY(case no_document when 2 then 3 else 1 end) [/PHP]

that???

It doesn't seems to make any difference,

get again document 1, document 2 and document 3
Newbie
 
Join Date: Oct 2008
Posts: 10
#6: Oct 22 '08

re: Problem with ORDER BY


I found the solution by reading in MYSQL Documentation.... Here is the solution :

[PHP]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')[/PHP]
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#7: Oct 23 '08

re: Problem with ORDER BY


Quote:

Originally Posted by Emmash

[PHP]SELECT no_document,titre_document FROM document WHERE no_document=2 OR no_document=3 OR no_document=1 ORDER BY(case no_document when 2 then 3 else 1 end) [/PHP]

that???

It doesn't seems to make any difference,

get again document 1, document 2 and document 3

Your case should have been when 3 then 1 when 1 then 2 when 2 then 3 but that's not so important now because the solution you found is much cleaner.
Reply