5
In PHP I am making a query to my database using Mysql. In my database I have the table fotos_album
and inside I have the name of the photo. I want to sort the table by field nome
.
We imagine that the table has the following data:
id nome
1 corrida (1 de 140)
2 corrida (20 de 140)
3 corrida (5 de 140)
4 corrida (2 de 140)
The purpose is that my ordination be as follows::
id nome
1 corrida (1 de 140)
4 corrida (2 de 140)
3 corrida (5 de 140)
2 corrida (20 de 140)
It is not enough to use ORDER BY nome ASC
, then how can I make the ordination correct?
Edited: The name is entered by the user. If he enters the name field as I wrote above, a simple sort does not work
I tried the following, but it doesn’t work properly in all cases:
SELECT *
FROM fotos_album
WHERE id = '$id_noticia'
ORDER BY
SUBSTR(nome,
LEAST(
CASE WHEN LOCATE('0', nome)>0 THEN LOCATE('0', nome) ELSE LENGTH(nome) END,
CASE WHEN LOCATE('1', nome)>0 THEN LOCATE('1', nome) ELSE LENGTH(nome) END,
CASE WHEN LOCATE('2', nome)>0 THEN LOCATE('2', nome) ELSE LENGTH(nome) END,
CASE WHEN LOCATE('3', nome)>0 THEN LOCATE('3', nome) ELSE LENGTH(nome) END,
CASE WHEN LOCATE('4', nome)>0 THEN LOCATE('4', nome) ELSE LENGTH(nome) END,
CASE WHEN LOCATE('5', nome)>0 THEN LOCATE('5', nome) ELSE LENGTH(nome) END,
CASE WHEN LOCATE('6', nome)>0 THEN LOCATE('6', nome) ELSE LENGTH(nome) END,
CASE WHEN LOCATE('7', nome)>0 THEN LOCATE('7', nome) ELSE LENGTH(nome) END,
CASE WHEN LOCATE('8', nome)>0 THEN LOCATE('8', nome) ELSE LENGTH(nome) END,
CASE WHEN LOCATE('9', nome)>0 THEN LOCATE('9', nome) ELSE LENGTH(nome) END
)
) + 0
I think that to perform such ordering you should rethink how is your table in Mysql, because it is not normalized, so your development will be much more complex. To have a basis, give a look here: http://www.luis.blog.br/normalize-data-as-formas-normals.aspx
– Marciano.Andrade
No, the user puts in that name. He could put in someone else. The user wants it to always be ordered as I ask, but if he enters the name field as I put it on top, the simple sorting does not work
– pc_oc
What you might have in the field
nome
? Onlycorrida (M de N)
or anything? For example, the name can beaa
,b
,15
or140
? I see you’ve already accepted an answer, but I don’t think it works in the general case - just for this particular example. (in other words, if you’re looking for an "there windows explorer" sort - where the numbers appear in ascending order and the letters in lexicographic order - the solution will certainly be more complex than expected, if there really is a possible solution...)– mgibsonbr
yes, you can have anything. but advise another resolution?
– pc_oc
Okay, let’s think then... you can ensure that the customer will always insert following this template:
corrida (x de y)
? Is there any way you could use a mask for that in the client input? if you can guarantee this, we can think of a way to solve using only SQL, however complex, it is possible.– Marciano.Andrade
no, I can’t guarantee that it will always insert in the running format (x de y)
– pc_oc
I know it’s not working yet, but it’s for the staff to do the tests. http://sqlfiddle.com/#! 2/09281/2
– Marcelo Diniz
Here is a continuation of the example and if possible, ask your client to enter the data in the form of this example that will work what you need. http://sqlfiddle.com/#! 2/477a8/1
– Marcelo Diniz