-2

I am in a similar case. despite many tests I find the same as Haleemur Ali my problem is that I need to sort in descending order I don't have a + or -

I find the same results as this post but I don't want that Haleemur Ali answer

...
AAC
AAB
AAA
...
AA
Z
...
B
A

this series of letters is found in a column of my database (MariaDB)

i would increment by one the last value found in my example i should find AAC +1 = AAD

I work with PHP and Laravel

Thanks for your help


olivier allowed me to make my request more elegant I post here if it can help

$lastL = \App\Models\MyTable::select('letter')
                              ->whereNotNull('letter')
                              ->orderByRaw("length(letter) DESC, letter DESC")
                              ->first();

if(is_null($lastL->letter)){
    return 'A';
}else{
    $letter = ++$lastL->letter;
    return $letter;
}
iDev7
  • 3
  • 2
  • 4
    I'm confused. You're referencing another question that has several answers, one of which was accepted, and stating that you have the same problem. Wouldn't the solution given in the answer then work to solve your problem? If not, please specify how so. Because I am at a loss as to what your question is specifically. – Sherif Feb 09 '20 at 10:50
  • my bad https://stackoverflow.com/questions/28814730/sort-values-that-contain-letters-and-symbols-in-a-custom-order/60135651#60135651 – iDev7 Feb 09 '20 at 10:57
  • it would be this solution https://stackoverflow.com/questions/43681099/sorting-array-of-string-in-a-specific-format-a-z-aa-zz but in php – iDev7 Feb 09 '20 at 11:00
  • This works (up to a point); it must be the same logic in application code (albeit different syntax) `SELECT UNHEX(HEX('AAC')+1);` – Strawberry Feb 09 '20 at 11:25
  • Does this answer your question? [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – TylerH Feb 09 '20 at 20:30

1 Answers1

0
SELECT * FROM table
ORDER BY LENGTH(value) DESC, value DESC

See the SQL Fiddle here.

Olivier
  • 13,283
  • 1
  • 8
  • 24
  • thank you so much I just found a less elegant solution inspired by this post https://forums.mysql.com/read.php?10,71177,71211 I post the modification under the report if it helps someone else – iDev7 Feb 09 '20 at 11:41