473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with ORDER BY

10
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
Oct 22 '08 #1
6 1501
r035198x
13,262 8TB
That's what the ORDER BY clause is used for. You did not include it in your query.
Oct 22 '08 #2
Emmash
10
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.
Oct 22 '08 #3
r035198x
13,262 8TB
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.
Oct 22 '08 #4
Emmash
10
[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
Oct 22 '08 #5
Emmash
10
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]
Oct 22 '08 #6
r035198x
13,262 8TB
[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.
Oct 23 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Fnark! | last post by:
I am creating a shopping cart using PHP Version 4.1.2. I am creating and registering a cart object in a session. The cart object contains an array of arrays called $order whose elements are a...
19
by: Lauren Quantrell | last post by:
I have a stored procedure using Convert where the exact same Convert string works in the SELECT portion of the procedure but fails in the WHERE portion. The entire SP is listed below....
9
by: jwedel_stolo | last post by:
Hi I'm creating a dataview "on the fly" in order to sort some data prior to writing out the information to a MS SQL table I have used two methods in order to determine the sort order of the...
8
by: Tom Szabo | last post by:
cTemp = oNode.cName; aStatList = "term"; window.alert(aStatList); -> "term" window.alert(aStatList.length); -> "0" the above code is going to display "term" and "0" ...
5
by: NM | last post by:
Hi All, I am having a peculiar problem. I used to link between intel fortran 7.0 and g++ without any problem. Now the intel compiler is upgraded to version 9.0 and I am getting segmenatation...
11
by: Geoff Jones | last post by:
Hi I have a table that has a column with Date types. I am trying to view certain rows in the table using a DataView. Using the filter, I can view the rows with, for example, the date equal...
20
by: Development - multi.art.studio | last post by:
Hello everyone, i just upgraded my old postgres-database from version 7.1 to 7.4.2. i dumped out my 7.1 database (with pg_dump from 7.1) as an sql-file with copy-commands and to one file using...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
4
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
28
tdw
by: tdw | last post by:
Hi all, I am trying to add a feature to our database that automates sending emails. When entering a new order, I want the option to send an email to the company the order came from, attaching a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.