在VIEW sql中显示不同的变量

时间:2016-12-01 16:58:22

标签: sql oracle select view

创建视图:

 CREATE OR REPLACE VIEW viewHolidayID AS
    Select Package_Code, Description, Country_Visted
    FROM tbl_Holiday_Details

选择查询:

Select * from viewHolidayID
WHERE Country_Visted 

在Country_Visted列中显示国家/地区列表。我试图做的,而不是单独展示各国,显示了大陆。 例如,'中国'将展示'亚洲','印度'将展示'亚洲','意大利'将展示'欧洲'。我不希望它永久更改变量,只是为了在使用选择查询时显示

1 个答案:

答案 0 :(得分:0)

这可能是一种“简单”的方式:

select Package_Code,
       Description,
       Country_Visted,
       case (Country_Visted)
         when 'Italy' then 'Europe'
         when 'India' then 'Asia'
         when 'China' then 'Asia'
       end as continent_visited
from tbl_Holiday_Details

但是,这是一种非常糟糕的方法,因为每次添加新国家时都必须编辑代码以处理新值。

更好的解决方案是定义一个新表来存储每个国家的大陆并将其加入到您的桌子中:

create table CountryContinent(country, continent) as
(
select 'Italy', 'Europe' from dual union all
select 'India', 'Asia' from dual union all
select 'China', 'Asia' from dual
)       

select Package_Code,
       Description,
       Country_Visted,
       c.continent
from tbl_Holiday_Details h
     inner join CountryContinent c
     on (h.Country_Visted = c.country)

这样,只需在表格中出现新国家/地区时在表格中添加一行,而无需修改代码