Oracle查询获取国家/地区列表

时间:2014-05-02 11:32:15

标签: sql oracle oracle11g oracle10g

我使用的是oracle 10g,我的表名是列名firstname,lastname,country和region。

country | region
--------|----------
India   | Asia
China   | Asia
Ireland | Europe
England | Europe

以下是senarios,

  1. 当我们为国家和地区传递“ALL”时,输出查询应为

    select * from Test_Table
    
  2. 当我们通过“印度,中国”为国家和“亚洲,欧洲”为区域时,输出查询应该是

    select * from Test_Table where counrty in (Malayasia,japan) and region in (Asia , Europe)
    
  3. 如何将其组合到传递值的单个查询中,将非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

如果将in作为参数传递,则无法使用select * from Test_Table where (:v_countries = 'ALL' or ','||:v_countries||',' like '%,'||country||',%') and (:v_regions = 'ALL' or ','||:v_regions||',' like '%,'||region||',%'); 。最简单的方法是构造一个创建上述查询的查询字符串。这也具有Oracle可以利用查询索引的优势。

您也可以使用字符串操作表达您想要做的事情:

where

但是,我建议您使用应用程序逻辑来创建正确的{{1}}子句,而不是使用此技巧。

相关问题