使用MATCH AGAINST将查询从MySQL转换为Oracle

时间:2013-09-02 22:23:30

标签: mysql sql database oracle oracle-text

我真的需要帮助以下用MySQL编写的查询我希望在Oracle pl / sql中进行转换。我读了一些Oracle文本文档,我想对于MySQL中的MATCH AGAINST我可以在Oracle中使用CONTAINS,但我在转换列得分,得分0和得分1时遇到问题。

SELECT table_id,
       MATCH(text) AGAINST('grand hotel') AS score,
       MATCH(text) AGAINST('grand') AS score0,
       MATCH(text) AGAINST('hotel') AS score1 
  FROM tbl
 WHERE MATCH(text) AGAINST('grand hotel')
 ORDER BY score ASC

1 个答案:

答案 0 :(得分:1)

我认为您在问题中提到的文档是Oracle Text,并且您已经对该功能有所了解。你也没有理由说PL / SQL应该被涉及,所以下面是一个简单的简单的SQL例子,应该解决你的问题:

数据

create table so32 as
select 1 as id, 'Lorem grand ipsum dolor sit amet, consectetur adipiscing elit. Cras faucibus.' as text from dual union all
select 2 as id, 'Lorem ipsum hotel dolor sit amet, consectetur adipiscing elit. Cras faucibus.' as text from dual union all
select 3 as id, 'Lorem ipsum dolor sit amet, grand consectetur adipiscing elit. Cras faucibus.' as text from dual union all
select 4 as id, 'Lorem ipsum dolor sit amet, consectetur hotel adipiscing elit. Cras faucibus.' as text from dual union all
select 5 as id, 'Lorem ipsum dolor sit amet, consectetur adipiscing elit grand. Cras faucibus.' as text from dual union all
select 6 as id, 'Lorem ipsum dolor sit amet grand hotel, consectetur adipiscing elit. Cras faucibus.' as text from dual
;

Oracle Text index

create index so32_index on so32(text) indextype is ctxsys.context;

查询

select id, 
       score(1) as grand,
       score(2) as hotel,
       score(3) as grandhotel
  from so32
 where contains(text, 'grand', 1) > 0
    or contains(text, 'hotel', 2) > 0
    or contains(text, 'grand hotel', 3) > 0
 order by score(3), score(2), score(1)
;

<强>结果

        ID      GRAND      HOTEL GRANDHOTEL
---------- ---------- ---------- ----------
         1          4          0          0
         3          4          0          0
         5          4          0          0
         4          0          4          0
         2          0          4          0
         6          4          4          4

6 rows selected.

希望这有帮助!