DarkNova
DarkNova

Reputation: 3

Custom Sort order PHP + MySQL

I have these 2 columns in the table and my PHP code on my site is connected to it.

username | primary_group

I currently have it being sorted by username but how would I make it sort by primary_group with the groups in a certain order without making a sort column in the group name table and link the tables?

Currently, I have:

SELECT username, primary_group FROM table_name ORDER BY username.

Upvotes: 0

Views: 247

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522741

You could use MySQL's ORDER BY FIELD capability to specify a certain custom order to use when sorting on primary_group:

SELECT
    username, primary_group
FROM
    table_name
ORDER BY
    FIELD(primary_group, 'group1', 'group2', 'group3', ...)

Upvotes: 1

Related Questions