I have two columns named priority
and state
:
priority
will only contain field values: urgent
and normal
.state
will only contain field values: wait
, executed
and done
.I'm trying to sort these using my dataset CommandText
property, so the dbgrid
connected to the dataset displays the data in the sort order I've set there.
The sort order should be like this:
urgent
in the priority
column should start the DBGrid list.normal
in the priority
column,wait
in the state
column,executed
in the state
column,done
in the state
column.I can't figure out how to write the SQL statement for this, so I can use it in the CommantText
property for the dataset.
I am using this currently:
SELECT *
FROM table_name
ORDER BY FIELD(column_name, "normal", "urgent") DESC
This works with the first column priority
but doesn't take into account the second column state
.
You are very close. You just need a second key in the order by
:
SELECT *
FROM table_name
ORDER BY FIELD(priority, 'urgent', 'normal'),
FIELD(state, 'wait', 'executed', 'done')
Notes:
DESC
from the first key. You are using field()
, so put things in the right order.