我正在Oracle中练习一些例子,因为我是新手。我想显示在下面的表的最后和最后匹配的WHERE子句行的First WHERE子句匹配的行。
CREATE table demo(id int, name varchar(50), city varchar(20), zip varchar(20));
insert into demo values(1,'oliver71','munich','85716');
insert into demo values(2,'markus71','munich','85716');
insert into demo values(3,'oliver81','stuttgart','70024');
insert into demo values(4,'peter82','hale','81978');
insert into demo values(5,'chinnarose71','rosenheim','80324');
insert into demo values(6,'guru11','berlin','81982');
insert into demo values(7,'sachin51','frankfurt','00712');
select name, city, zip from demo where zip like '%7%' OR city LIKE '%7%'
OR name LIKE '%7%';
我如何在Oracle中做到这一点。任何建议将不胜感激。
答案 0 :(得分:3)
您的查询中没有“WHEN”子句。我假设你的意思是WHERE子句的部分。
为此,您需要一种排序结果的方法。这是使用子查询的一种方法,我认为这可能是最清楚的:
select name, city, zip
from (select name, city, zip,
(case when zip like '%7%' then 1
when city LIKE '%7%' then 2
when name LIKE '%7%' then 3
else 0
end) as matchitem
from demo
) d
where matchitem > 0
order by matchitem