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

标签 php mysql database postgresql

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

<小时/>

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/

相关文章:

php - PHP + javascript 世界中的 dot net + flex 是什么?

php - 加快论坛转换

php - 如何在 mySql 查询中使用 mysql 列内容作为 php 数组的索引(键)?

MySQL:如何选择购买了带有附加配件的产品的客户

mysql - 无法连接到mysql服务器数据库

android - 无法在 Realm.io 中处理 RealmList<RealmList<RealmInt>>

php - 将两个旧表中的数据插入到新表中

php - 不含 FOSUserBundle 的 HWIOAuthBundle

php - 在一个php脚本中使用多个数据库

mysql - 在mysql中连接具有多个外键的表