order by specific text

Asked

Viewed 114 times

0

How do I make a order by by text criterion?

I have a column with some ratings and wanted it to come in the order I determine.

Example:

'carro1'
'carro3'
'carro5'
'carro10'
'carro2'
  • Maikol, the idea has not yet become clear.. could expose more details?

  • and what would be the order you determine ?

  • Oops. I have a column with some ratings and wanted to display the query result with the ratings in the order I determine.

  • 1
  • 2

    For your specific case, just take substring and cast to int, that car 10 will come at the end. But the real problem is not that, probably. This is known as XY Problem, which is when you ask about the solution you think will solve, rather than exposing the real problem.

  • I think the same problem is how to store the data... "display the result in the order I determine" if it is to determine the order of each item, better create a column and sort by it

  • Rovann, I did that and it was the solution. thanks, abs.

Show 2 more comments

1 answer

3


You could create a ranking column.

From what I understand there are few records, so creating a classification column, you could give a numbering for each of the record of your listing, so in the future if any in the category appears, just edit in the bank, or even make an administrative menu for such a purpose.


But if you still want to filter specifically by the cited records, follow an example of a "temporary Palliative Emergency Solution", alias "gambiarra".

SELECT
  carName
FROM
  exemple

ORDER BY
CASE carName
    WHEN 'carro1' THEN 0
    WHEN 'carro3' THEN 1
    WHEN 'carro5' THEN 2
    WHEN 'carro10' THEN 3
    WHEN 'carro2' THEN 4
    ELSE 999
END

Testable example: http://sqlfiddle.com/#! 18/49da0/11

  • David, thank you, it worked. Abs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.