在 Oracle 11.2 中包含

CONTAINS in Oracle 11.2

是否可以使用新 Oracle 中的 CONTAINS 函数之类的东西? 我有 11.2 并且想这样做:

select * from cars
inner join customers on customers.as_id = cars.as_id
where cars.type like 'AUDI' and contains(request, customers.name, 1) > 0;

所以我知道我不能在这里使用 LIKE 因为 customers.name 不是固定值。 有没有办法为旧的 Oracle 找到一些解决方法?

您可以按如下方式使用LIKE

select * 
  from cars
 inner join customers on customers.as_id = cars.as_id
 where cars.type = 'AUDI' -- You can use = here
   and request like '%' || customers.name || '%';

注意: contains 子句用于在 Oracle Text 索引中查找特定字符串。它不能应用于普通列。

CONTAINS是一个Oracle文本函数; Oracle 11g 支持 Oracle Text。

只需在列上创建一个 Oracle 文本上下文索引:

CREATE TABLE cars ( as_id, type, request ) AS
SELECT 1, 'AUDI', 'Requested by alice abbots on 2020-01-07' FROM DUAL;

CREATE INDEX cars__request__textidx ON cars(request) INDEXTYPE IS CTXSYS.CONTEXT;

CREATE TABLE customers ( as_id, name ) AS
SELECT 1, 'Alice Abbots' FROM DUAL;

那么您的查询:

select * from cars
inner join customers on customers.as_id = cars.as_id
where cars.type like 'AUDI' and contains(request, customers.name, 1) > 0;

输出:

AS_ID | TYPE | REQUEST                                 | AS_ID | NAME        
----: | :--- | :-------------------------------------- | ----: | :-----------
    1 | AUDI | Requested by alice abbots on 2020-01-07 |     1 | Alice Abbots

db<>fiddle here