如何按列中可能存在或不存在的数据拆分查询结果?
How to split query result by data which may be present or absent in the column?
我的查询结果如下:
-------------------
| id | c1 | c2 |
-------------------
| 1 | 3 | 4 |
-------------------
| 2 | 4 | 3 |
-------------------
| 3 | 5 | 4 |
-------------------
我希望 c2
将 3 作为第一行(3 来自另一个查询),如下所示:
-------------------
| id | c1 | c2 |
-------------------
| 2 | 4 | 3 |
-------------------
| 1 | 3 | 4 |
-------------------
| 3 | 5 | 4 |
-------------------
但其他行也必须如此。
你可以只ORDER BY
一个布尔表达式:
...
ORDER BY (c2 = 3) DESC NULLS LAST, id;
相关(带解释):
- Sorting null values after all others, except special
我的查询结果如下:
------------------- | id | c1 | c2 | ------------------- | 1 | 3 | 4 | ------------------- | 2 | 4 | 3 | ------------------- | 3 | 5 | 4 | -------------------
我希望 c2
将 3 作为第一行(3 来自另一个查询),如下所示:
------------------- | id | c1 | c2 | ------------------- | 2 | 4 | 3 | ------------------- | 1 | 3 | 4 | ------------------- | 3 | 5 | 4 | -------------------
但其他行也必须如此。
你可以只ORDER BY
一个布尔表达式:
...
ORDER BY (c2 = 3) DESC NULLS LAST, id;
相关(带解释):
- Sorting null values after all others, except special