如何在子查询上使用LIKE通配符?

时间:2018-08-14 15:41:34

标签: sql oracle

Table: FirstNames
NAME
    Tom
    Joe
    Peter

Table: FullNames
FULL_NAME:
    Tom Petty.
    Joe Satriani.
    Peter Griffin.
    Sarah Connor.

我想运行一个查询:

select * 
from FullNames where FULL_NAME like '%' || (select NAME from FirstNames) || '%'

它产生:

  

ORA-01427:单行子查询返回多个行

这似乎是正确的。在Oracle中有没有办法做到这一点?

2 个答案:

答案 0 :(得分:5)

您可以使用JOIN

SELECT * 
FROM FullNames f
JOIN FirstNames g
  ON f.FULL_NAME LIKE '%' || g.NAME || '%';

答案 1 :(得分:4)

您可以使用exists

select f.*
from FullNames f
where exists (select 1
              from firstnames fn
              where f.FULL_NAME like '%' || fn.NAME || '%'
             );