sql命令查询定期

时间:2014-08-16 12:24:58

标签: sql sql-order-by

我的表包含国家,城市,街道,门号等。在每条街道我都有三种类型的地址。其中一个是主要(永久),其他是替换(临时)1和替换(临时)2。

我想按城市和街道订购

我的结果如下:

City       street             address type

sincan     plevne street      replacement1
sincan     plevne street      main
sincan     plevne street      replacement2
sincan     bosna  street      replacement2
sincan     bosna  street      main
sincan     bosna  street      replacement1
sincan     yazi   street      replacement2
sincan     yazi   street      replacement1
sincan     yazi   street      main

但是我想订购它们就像跟随它一样。

sincan     plevne street      main
sincan     plevne street      replacement1
sincan     plevne street      replacement2
sincan     bosna  street      main
sincan     bosna  street      replacement1
sincan     bosna  street      replacement2
sincan     yazi   street      main
sincan     yazi   street      replacement1
sincan     yazi   street      replacement2

有可能吗?

由于

1 个答案:

答案 0 :(得分:0)

CASE子句

中使用ORDER BY表达式
ORDER BY city, street, 
    CASE adress_type
        WHEN 'main' THEN 1
        WHEN 'replacement1' THEN 2
        WHEN 'replacement2' THEN 3
        ELSE 4
    END