Oracle SQL,在一列中搜索包含来自另一列的字符串的字符串

时间:2018-04-12 00:19:20

标签: sql database oracle subquery

目前有这个表,并且需要能够找到包含人名的

的句子
ID         FIRSTNAME       LASTNAME        EMAIL                         PHRASE
--------  --------------- ------------     ---------------------------   ---------------------------------------------------------------          
1          John            Smith           jsmith@faker.com              I'm the same as other people.
2          Sarah           Lane            saralane23@faker.com          I'm different from other people
3          Allen           Borazt          allenB@faker.com              I guess I need another job.
4          Mike            Himmy           mh@faker.com                  We have a great time. John Smith really enjoys his time out.
5          Jillian         Carters         jcartrs@faker.com             What happened to the car? 
6          Steven          Oarts           SteveO2@faker.com             Just chillin' over in the pool
7          Ronald          Donalds         don@faker.com                 Just in case you were wondering
8          Nancy           Arist           nArist@faker.com              What I really want is Paul Blarts movie.
9          Paul            Blart           paulb@faker.com               I think Nancy Arist hair color is cool.

现在我的查询看起来像这样,但它返回"没有选择行"。

SELECT T.*
FROM Table T
WHERE T.Phrase Like '%' || T.FirstName || '%';

我想要的结果会是这样的。

ID         FIRSTNAME       LASTNAME        EMAIL                         PHRASE
--------  --------------- ------------     ---------------------------   ------------------------------------------------------------------------------------------------------------------------               
4          Mike            Himmy           mh@faker.com                  We have a great time. John Smith really enjoys his time out.
8          Nancy           Arist           nArist@faker.com              What I really want is Paul Blarts movie.
9          Paul            Blart           paulb@faker.com               I think Nancy Arist hair color is cool.

3 个答案:

答案 0 :(得分:1)

Hmmmm。问题是您只查看单行中的值。我想你想要:

select t.*
from table t
where exists (select 1
              from table t2
              where t.Phrase Like '%' || t2.FirstName || '%'
             );

答案 1 :(得分:0)

执行子查询并使用exists来检查是否在短语列中找到了fname或lname。

SELECT A.*
FROM Table1 A
WHERE EXISTS (
          SELECT * 
           FROM Table1 B
           WHERE B.Phrase Like '%' || A.FirstName || '%'
           OR  B.Phrase Like '%' || A.LastName  || '%')

答案 2 :(得分:0)

  

添加查询以忽略区分大小写

SELECT T1.*
FROM Table T1, Table T2
WHERE upper(T1.Phrase) Like '%' || upper(T2.FirstName) || '%'
OR upper(T1.Phrase) Like '%' || upper(T2.LastName) || '%';