I hope there's a simple solution for this:
I have a table where each row has it's own status (SET
type field). Statuses can be:
I'd like to order as follows
I thought a simple
ORDER BY `status` ASC
will do the trick (alphabetical order) but it gives me the following:
How can is sort out this in the most simple way?
Thanks in advance,
fabrik
select type , COUNT from TABLE order by FIELD(type,'A','B','C','D') ; It works fine if the column type has value for 'A,B,C,D' . In some cases the order by FIELD('A','B','C','D') some columns may not have value in table . In this cases I want to put 0 for it and construct a result .
An "ALTER TABLE ORDER BY" statement exist in the syntaxes accepted by MySQL. According to the documentation, this syntax: - only accept *one* column, as in "ALTER TABLE t ORDER BY col;" - is used to reorder physically the rows in a table, for optimizations.
SELECT * FROM table ORDER BY FIELD(status,'offline','available','busy','distance')
see Mysql order by specific ID values
Thought I'd add another way to order by custom field values,
ORDER BY FIND_IN_SET(status, 'available,busy,distance,offline')
(If the given strings contain a quote simply escape it)
You could also do something like this, if reordering the SET
values in impractical:
... ORDER BY CASE `status`
WHEN 'available' THEN 1
WHEN 'busy' THEN 2
WHEN 'distance' THEN 3
WHEN 'offline' THEN 4
END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With