|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
MySQL, order by
I'm trying to sort a field and find it coming out like this:
Vol 1 Vol 10 Vol 11 Vol 12 Vol 2 Vol 3 Vol 4 Is there a way to get the rows in "correct" order? This is how I want it: Vol 1 Vol 2 Vol 3 Vol 4 [...] Vol 10 Vol 11 Vol 12 Btw, I'm using the "ORDER BY title" syntax. |
|
#2
|
|||
|
|||
|
RE: MySQL, order by
In that example the fields only contains numbers. For me this isn't the case so I don't think that will work.
|
|
#3
|
|||
|
|||
|
RE: MySQL, order by
Can you not remove the 'Vol' part of the data from each row? Ideally you should never repeat information in each row if it is always the same since it can be prefixed later.
There's probably a really easy way of modifying this. If not, you could try something along the lines of: Explode each field Sort by number Recompile field |
|
#4
|
|||
|
|||
|
RE: MySQL, order by
If you intend on keeping the "vol" part of the string then try labeling them as "vol 001", "vol 002", etc. Or if you do not intend on surpassing volume 999 then just use one leading zero (ex. vol 01) This should sort correctly.
|
|
#5
|
|||
|
|||
|
RE: MySQL, order by
Thanks everyone for the help...
The field that I'm sorting contains movietitles. Most titles doesn't contain numbers. But the problems ocurrs when it comes to the "Friends" episodes. It's a swedish site but you can have a look at: http://www.prontofilm.com/list.php?c=2&p=9 |
|
#6
|
|||
|
|||
|
RE: MySQL, order by
You could try something along the lines of using eregi() to sort out the numbers in a title then sort by that and apply to the array.
|
|
#7
|
|||
|
|||
|
RE: MySQL, order by
Why not do it the easy way.
01 02 03 .. 10 The leading zero wil put it in order. |
|
#8
|
|||
|
|||
|
RE: MySQL, order by
The problem is that the `Vol` column is being sorted as a character string. So what you need to do it order it as a number as stated above. When you cast things from strings to ints in MySQL, it will read all leading numbers and count that as the new numeric value, ie. 12val would become 12, 123asdf12 would become 123 and so on.
Now you can see that there will be a problem with your strings as they start with Vol and not numbers. What you will need to do is to use a string function to remove the leading part. SUBSTRING should be able to do exactly what you want, or SUSTRING(column,5) will return just the numeric part. However it returns that value as a string, so now we will have to cast it to an integer as stated above, which will give you a final query of: SELECT * FROM table ORDER BY SUBSTRING(column,5)+0; Keep in mind that query can't use an index to order by, so it won't be very fast for large data sets. You would definately want to consider going with the idea as given by codegirl with the seperate volume number. |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > MySQL, order by |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|