0

I'm trying to make a query to get the list of a students class ordered by the type defined, sex and name.

I have the next query:

SELECT * FROM STUDENTS
  ORDER BY FIELD(type, 'b', 'p', 'j', 'i', 'a', 'c', 'v'), FIELD(sex, 'M', 'F'), `name` ASC

I'm getting that order, first type, and then both sex from that type and name, but I need to define some types and sex together, something like this:

type = 'b', sex = 'M', name
type = 'b', sex = 'F', name
type = 'p', sex = 'M', name
type = 'p', sex = 'F', name
type = 'i', sex = 'M', name
type = 'a', sex = 'M', name
type = 'c', sex = 'M', name
type = 'v', sex = 'M', name
type = 'j', sex = 'M', name
type = 'i', sex = 'F', name
type = 'a', sex = 'F', name
type = 'c', sex = 'F', name
type = 'v', sex = 'F', name
type = 'j', sex = 'F', name
Louis B
  • 306
  • 5
  • 18

1 Answers1

1

You can always use a CASE statement to create the expression you want

SQL DEMO

SELECT *
FROM Table1
ORDER BY CASE WHEN type = 'b' and sex = 'M' THEN 1
              WHEN type = 'b' and sex = 'F' THEN 2
              WHEN type = 'p' and sex = 'M' THEN 3
              WHEN type = 'p' and sex = 'F' THEN 4
              ....
              ELSE 9999
          END,
          name
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118