1

SQL テーブルの順序について質問があります。すでにいくつかのことを試しましたが、必要な解決策が見つかりません。

私のテーブルは次のようになります。

username  childs+pets  childs    pets
=======================================
Max       1                      1
Nico      3            1         2       
Lewis     2            2        
Daniel    2            1         1

テーブルを childs+pets (ASCending) で並べ替えたいのですが、フィールドが空のレコード (Max と Lewis) をテーブルの一番下に配置したいと考えています。結果は次のとおりです。

username  childs+pets  childs    pets
=======================================
Nico      3            1         2
Daniel    2            1         1       
Lewis     2            2        
Max       1                      1

誰が私を助けることができます?

4

3 に答える 3

0

サンプルは十分に明確ではないため、次の 2 つのいずれかを選択してください。

select      *

from        t

order by    "childs+pets" + 0  desc
           ,case when childs = '' then 2 else 1 end
           ,case when pets   = '' then 2 else 1 end
;          

select      *

from        t

order by    case when childs = '' then 2 else 1 end
           ,case when pets   = '' then 2 else 1 end
           ,"childs+pets" + 0 desc
;        
于 2016-11-15T09:27:52.657 に答える