I've got the following table Foobar that looks like this:
+----+-------------+
| ID | Description |
+----+-------------+
| 12 | aab |
+----+-------------+
| 13 | fff |
+----+-------------+
| 14 | fff |
+----+-------------+
| 15 | xab |
+----+-------------+
What I would like is to print out all the descriptions in order. However I would first of all like the values "fff" to be right at the top. In other words the output should be as follows: fff, fff, aab, xab.
So a simple: "SELECT foobar.description FROM foobar ORDER BY foobar.description ASC" will not work.
The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.
To fetch the first alphabet from the strings, use LEFT(). This method allows you to return characters from the left of the string.
So the order of columns in a multi-column index definitely matters. One type of query may need a certain column order for the index. If you have several types of queries, you might need several indexes to help them, with columns in different orders.
Use the ASC option to sort the result set in ascending order and the DESC option to sort the result set in descending order.
In MySQL this works
SELECT foobar.description
FROM foobar
ORDER BY foobar.description <> 'fff',
foobar.description ASC
But generally you can also use a case
SELECT foobar.description
FROM foobar
ORDER BY case when foobar.description = 'fff' then 1 else 2 end,
foobar.description ASC
select foobar.description from foobar
order by case when foobar.description = 'fff' then 1 else 2 end,
foobar.description asc
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