0

My query result is as follows:

-------------------
| id  | c1 |  c2  |
-------------------
|  1  |  3 |  4   |
-------------------
|  2  |  4 |  3   |
-------------------
|  3  |  5 |  4   |
-------------------

I want c2 with 3 be the first row (3 came from another query) like this:

-------------------
| id  | c1 |  c2  |
-------------------
|  2  |  4 |  3   |
-------------------
|  1  |  3 |  4   |
-------------------
|  3  |  5 |  4   |
-------------------

But other rows must also be.

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Stepan Pavlov
  • 119
  • 2
  • 3
  • 7
  • How about some `order by c2`? – mechanical_meat Jul 30 '16 at 03:53
  • Yes, but i want to use 3, and don't know how... – Stepan Pavlov Jul 30 '16 at 03:58
  • What have you tried so far? When you say "But other rows must also be", can you be a little more explicit? It's not clear what you want aside from the `order by` clause that @bernie suggested. – jmelesky Jul 30 '16 at 04:22
  • I want to split the query result into two items: First must have 3 in c2, second must not have 3 in c2. The only way i see is to make select to the result where c2 = 3 and another select where c2 != 3, lately append two selections one another. But it may become time consuming... – Stepan Pavlov Jul 30 '16 at 05:34
  • 1
    `... order by case when c2 = 3 then 1 else 2 end` – Abelisto Jul 30 '16 at 09:04

1 Answers1

1

You can just ORDER BY a boolean expression:

...
ORDER BY (c2 = 3) DESC NULLS LAST, id;

Related (with explanation):

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228