Oracle: merging two different queries into one, LIKE & IN

橙三吉。 提交于 2019-12-02 06:09:24

问题


I need to implement a search query, where we have multiple filters(values) for a single column in database(oracle). But these multiple filters(values) are LIKE query parameters. I am not sure that whether I am visioning correct result using the approach in mind.

I want something that should work like:

departmentname IN ( LIKE '%Medi%', LIKE '%Ciga%')

I know it will not work, just I want to show the vision I am having.

Though we all know that its simple using foreach and manually adding 'OR' between queries like:

WHERE DEPARTMENTNAME LIKE '%Medi%' OR '%Ciga%' OR '%Tobacc%'

But is there any way to implement it using IN() AND LIKE simultaneously?

Other suggestions are also welcomed.


回答1:


As was already commented, it is better and simpler to just concatenate several conditions:

where departmentName like '%Medi%'
   or departmentName like '%Ciga%'
   or departmentName like '%Tabacc%';

Another way is to insert those values '%Medi%', '%Ciga%' and '%Tabacc%' into a conditionTable, and then run this query:

select department.*
  from department
 cross join conditionTable
 where department.departmentName like conditionTable.value;

I am assuming here that your table is department and that the conditionTable has a column value. If you implement this solution, you should care about concurrency, and filter conditionTable by something like

select department.*
  from department
 inner join conditionTable on conditionTable.session = yourSessionId
 where department.departmentName like conditionTable.value;

Finally, a third solution that might be handy, if you dont want to use a conditionTable, is to generate a string select <cond1> as value from dual union select <cond2> from dual... and placed into a dynamic query as

select department.*
  from department
 cross join
   (select '%Medi%' as value from dual
     union
    select '%Ciga%' from dual
     union
    select '%Tabacc%' from dual) conditionTable
 where department.departmentName like conditionTable.value;


来源:https://stackoverflow.com/questions/10623088/oracle-merging-two-different-queries-into-one-like-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!