Category: Laravel, MySQL

The other day I needed to sort a dataset in MySQL and ensure one value was always at the end. However, one of them is “Other”, and I want that one to appear at the end of the list in my UI.

This is what the result of field(name, "Other") looks like for this dataset: | id | name | field | 1 | Backend Developer | 0 | 2 | Designer | 0 | 3 | Other | 1 | 4 | Account Manager | 0 | 5 | Frontend Developer | 0

When we run order by number, letter, MySQL will order by number first, and keep the subsets on their own little islands: | letter | number | | e | 1 | | b | 1 | | d | 2 | | c | 3 | | a | 3 |

Back to our jobs dataset, order by field(name, "Other") will push the “Other” value below.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners