7

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.

4

3 回答 3

18

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
于 2013-10-15T10:07:51.493 回答
2
select foobar.description from foobar
order by case when foobar.description = 'fff' then 1 else 2 end,
foobar.description asc 
于 2013-10-15T10:10:37.833 回答
0

Try this:

SELECT description
FROM foobar
ORDER BY description != 'fff', description

This works because in mysql, true is 1 and false is 0.

I have also removed all unnecessary characters from the query.

于 2013-10-15T10:17:17.023 回答