我从表中获取记录,比如说国家/地区,我总是需要在顶部添加几行(在结果集的第一个)
<小时/>ID国家
--------------------------
1 China
2 Japan
3 Taiwan
4 USA
5 Germany
6 Brazil
7 India
--------------------------
我想将日本和中国保留在列表顶部,然后对其余的进行排序
所以结果集总是应该是
----------------------
Japan
China
next country
next country
next country
next country
----------------------
有没有办法使用 CTE 或任何其他适合此要求的方式来做到这一点?
我使用的是 PostgreSQL 9.4
最佳答案
您可以选择这两个国家,然后添加其余国家,例如:
select * from table
where country ='China' or country = 'Japan'
union select * from table;
union
已为您删除重复的行。
希望这有帮助。
关于php - 粘性行: fetching some rows always on top of list in postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36757959/