1

Currently have this table, and need to be able to find Sentences that contain the name of a person

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

Right now my query looks like this but it returns "No Rows Selected".

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

My desired result would look like this.

ID         FIRSTNAME       LASTNAME        EMAIL                         PHRASE
--------  --------------- ------------     ---------------------------   ------------------------------------------------------------------------------------------------------------------------               
4          Mike            Himmy           [email protected]                  We have a great time. John Smith really enjoys his time out.
8          Nancy           Arist           [email protected]              What I really want is Paul Blarts movie.
9          Paul            Blart           [email protected]               I think Nancy Arist hair color is cool.
4
  • I don't understand your expected output. You may look into using full text search. Commented Apr 12, 2018 at 0:21
  • So basically finding any phrase that contains the firstname or lastname of a person Commented Apr 12, 2018 at 0:22
  • I cannot find the table and sample data for colimn1 Commented Apr 12, 2018 at 0:23
  • Sorry i was gonna post this with random table names, but thought it might be easier to read if i put data, just edited it Commented Apr 12, 2018 at 0:24

3 Answers 3

1

Do a sub query and use exists to check if that fname or lname is found in phrase column.

SELECT A.*
FROM Table1 A
WHERE EXISTS (
          SELECT * 
           FROM Table1 B
           WHERE B.Phrase Like '%' || A.FirstName || '%'
           OR  B.Phrase Like '%' || A.LastName  || '%')
Sign up to request clarification or add additional context in comments.

3 Comments

Phrase was defined in the same table, its all in one. Looking online it said I may need to use a subquery, but doing your solution still resulted in "No Rows Selected"
i didnt scroll to the right so dod not see the column phrase. i updated my answer
Nice!, Thanks so much this worked, just added a trim over the name parts and got my query
1

Hmmmm. The problem is that you are looking only at the values within a single row. I think you want:

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

Comments

0

Adding a query to ignore case sensitivity

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.