2

我有一个类似这样的查询

select city_desc from mst_city where upper(city_desc) like upper('%branch%')

它填充结果those which start with branchthose which contains branch. 以随机顺序。

我想订购它,以便结果首先显示所有搜索结果,those which start with branch然后显示所有those which contains branch.

我怎样才能做到这一点。我知道我可能不得不使用Order By条款。但我无法弄清楚

4

2 回答 2

6

尝试以下解决方案:

WITH mst_city AS
  (SELECT 'branch test' AS city_desc FROM dual
   UNION ALL SELECT 'test branch test' FROM dual
   UNION ALL SELECT 'a branch' FROM dual
   UNION ALL SELECT 'branch' FROM dual
   UNION ALL SELECT 'something different' FROM dual)
SELECT
    city_desc
  FROM (
    SELECT
        city_desc,
        CASE
          WHEN UPPER(city_desc) LIKE 'BRANCH%' THEN 1
          ELSE 2
        END AS match_order
      FROM mst_city
    WHERE
      UPPER(city_desc) LIKE '%BRANCH%'
    )  
ORDER BY match_order;
于 2013-10-21T07:40:04.277 回答
4

您所需要的只是一个带有“BRANCH%”条件的排序顺序:

select city_desc from mst_city where upper(city_desc) like '%BRANCH%'
order by case when upper(city_desc) like 'BRANCH%' then 1 else 2 end
于 2013-10-21T09:00:11.173 回答