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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners