Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql - order by certain strings

Tags:

mysql

I have a players table. Each player has a position which is either goalkeeper, defender, midfielder or forward. I want to return all players, with forwards first, then midfielders, then defenders, then goalkeepers. Obviously, if I do:

SELECT * 
FROM  'Player' 
ORDER BY position

the defenders will be returned first....

like image 230
user1716672 Avatar asked Jun 15 '26 11:06

user1716672


1 Answers

Use FIND_IN_SET()

SELECT * 
FROM Player 
ORDER BY find_in_set(position,'forward,midfielder,defender,goalkeeper')
like image 103
juergen d Avatar answered Jun 17 '26 02:06

juergen d



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!