Geover Zamora
Geover Zamora

Reputation: 26

MariaDB: to order by specific Column_A value and Column_B

I have a table like:

+-------------------+-----------+
| FieldId           | FieldValue|
+-------------------+-----------+
| field_klsciv50    | Apple     |
| field_kbgankim    | Fish      |
| field_klsciv50    | Banana    |
| field_kijagc5r    | Cow       |
| field_kijagc5r    | Dog       |
| field_klsciv50    | Orange    |
+-------------------+-----------+

How do I make it so the order is:

+-------------------+-----------+
| FieldId           | FieldValue|
+-------------------+-----------+
| field_klsciv50    | Apple     |
| field_klsciv50    | Banana    |
| field_klsciv50    | Orange    |
| field_kijagc5r    | Cow       |
| field_kijagc5r    | Dog       |
| field_kbgankim    | Fish      |
+-------------------+-----------+

Can't figure out how to order it by Fruit Field with its FieldId of field_klsciv50 and the values above.

Thanks!

Upvotes: 1

Views: 48

Answers (1)

Frank
Frank

Reputation: 313

You can split the table on a FieldId value, sort the half that you wantand then combine them again:

SELECT FieldId, FieldValue
FROM
    (SELECT FieldId, FieldValue
     FROM MyTable
     WHERE FieldId = 'field_klsciv50'
     ORDER BY FieldValue
    )
UNION ALL
SELECT FieldId, FieldValue
FROM MyTable
WHERE FieldId != 'field_klsciv50'

Upvotes: 1

Related Questions