Jump to content

Output not sorted correctly


stefands

Recommended Posts

Hello all,

I've defined an index over 3 columns called "cprgpt" in table "country1". The columns in question being "camp", "region" and "postal". The $wherevar has the dynamic generated WHERE statement.

[code]$query = "SELECT * FROM country1 $wherevar ORDER BY camp, region, postal LIMIT $offset, $limit"; [/code]

The output of the SELECT statement provides the data looked for, the problem is that it's only sorted by camp, not followed by the region and then by postal codes. What could be the problem here?


PS: There is no other index that has "camp" and first key. The primary key index for the table is on another column (campid).
I've also read the following info:
[url=http://dev.mysql.com/doc/refman/4.1/en/order-by-optimization.html]http://dev.mysql.com/doc/refman/4.1/en/order-by-optimization.html[/url]
Link to comment
Share on other sites

[tt]
camp                          | region        | postal
------------------------------------------------------------------
Camping Athens                | Attica         | GR-12136
Camping Bacchus                | Attica         | GR-19550
Camping Delphi                | Cent. Greece  | GR-33054
Camping Enjoy Lichnos          | Epirus        | GR-48060
Camping Hellas International  | Thessaly      | GR-38500
Camping Kalami Beach          | Epirus         | GR-46100
Camping Kokkino Limanaki      | Attica        | GR-19009
Camping Sikia                  | Thessaly      | GR-38500
Camping Valtos                | Epirus        | GR-48060
Chrissa Camping               | Cent. Greece  | GR-33054
Ionion Beach                  | West Greece    | GR-27050[/tt]
Link to comment
Share on other sites

The sorted output given in your last post is correct (as specified in the SQL query).

But it's not really good sample data since there's not more than one camp with the same name to really tell that it's sorting it correctly. And would there ever be a camp with the same name in the same region?

If the first three rows listed were all the same camp name, you'd still get them in that order. Example:

camp                          | region        | postal
------------------------------------------------------------------
Camping Athens                | Attica          | GR-12136
Camping Athens                | Attica          | GR-19550
Camping Athens                | Cent. Greece  | GR-33054
...

See what I mean?  Maybe you meant to sort by region or zip, then by name?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.