gpt4 book ai didi

php - 粘性行: fetching some rows always on top of list in postgresql

转载 作者:行者123 更新时间:2023-11-29 07:19:16 25 4
gpt4 key购买 nike

我从表中获取记录,比如说国家/地区,我总是需要在顶部添加几行(在结果集的第一个)

<小时/>

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com