首页 > 解决方案 > 我可以应用“NULLS LAST”之类的东西,但不按非空的列值排序吗?

问题描述

想象一下我有疑问

 SELECT  "somethings.* 
 FROM "somethings" 
 ORDER BY description DESC NULLS LAST, popularity DESC`

它将首先对描述进行排序,将所有带有null描述的行放在最后,然后应用流行度排序。

但我想要的只是将具有空描述的行放在最后,而不是对描述字符串的主要结果进行排序,然后根据受欢迎程度对结果进行排序。

理想情况下,我想在一个查询中实现它。

标签: sqlpostgresql

解决方案


您可以使用is nullinorder by子句:

order by (case when description is null 
               then 1 else 0 end), description desc, popularity desc;   

推荐阅读