在 Oracle 中按指令排序
Order by instr in Oracle
我需要根据搜索条件以特定方式对查询进行排序。我有适用于 db2 的搜索顺序,但我还需要让它适用于 Oracle。
我对 db2 的查询使用定位函数。
Select *
from <table>
where UPPER(NAME) like "'%<search>%'
order by locate('<search>', UPPER(NAME)),
UPPER(NAME)
这将 return 搜索评论为:
评论,
审查任务,
审查 B 任务,
另一条评论
我曾尝试在 oracle 中使用 instr 函数,但它并不是我所希望的return。
select *
from <table>
where UPPER(name) like '%<search>%'
order by instr('<search>',UPPER(name)),
name
关于我需要做什么才能获得我正在寻找的订单有什么想法吗?
你把指令中的参数弄错了。您当前正在搜索词中查找模式 UPPER(nane)
。以下对我有用:
with sample_data as (select 'Review' name from dual union all
select 'Review A Task' name from dual union all
select 'Review B Task' name from dual union all
select 'Another Review' name from dual)
select *
from sample_data
where UPPER(name) like '%REVIEW%'
order by instr(UPPER(name), 'REVIEW'),
name;
NAME
--------------
Review
Review A Task
Review B Task
Another Review
我需要根据搜索条件以特定方式对查询进行排序。我有适用于 db2 的搜索顺序,但我还需要让它适用于 Oracle。
我对 db2 的查询使用定位函数。
Select *
from <table>
where UPPER(NAME) like "'%<search>%'
order by locate('<search>', UPPER(NAME)),
UPPER(NAME)
这将 return 搜索评论为:
评论, 审查任务, 审查 B 任务, 另一条评论
我曾尝试在 oracle 中使用 instr 函数,但它并不是我所希望的return。
select *
from <table>
where UPPER(name) like '%<search>%'
order by instr('<search>',UPPER(name)),
name
关于我需要做什么才能获得我正在寻找的订单有什么想法吗?
你把指令中的参数弄错了。您当前正在搜索词中查找模式 UPPER(nane)
。以下对我有用:
with sample_data as (select 'Review' name from dual union all
select 'Review A Task' name from dual union all
select 'Review B Task' name from dual union all
select 'Another Review' name from dual)
select *
from sample_data
where UPPER(name) like '%REVIEW%'
order by instr(UPPER(name), 'REVIEW'),
name;
NAME
--------------
Review
Review A Task
Review B Task
Another Review