vatsal
vatsal

Reputation: 63

Searching a word dictionary in a table in sql

I have two tables.

1st is sentence having title column with values.

my name is A.
B is my name.
I am C kind.
I am nice.

2nd is name having val column with values.

B.
C.
A.
D.
H.

I want to write a query which will return those titles which contains any val from name table. Also i want those val which are present in title of sentence table. How can i achieve this using select statement.

Upvotes: 1

Views: 322

Answers (1)

Barmar
Barmar

Reputation: 782064

You can join the tables with a LIKE operator.

To get all the titles that have any val in them:

SELECT DISTINCT(s.title) AS title
FROM sentence AS s
JOIN name AS n ON s.title LIKE CONCAT('%', n.val, '%');

To get all the vals that are in any of the titles:

SELECT DISTINCT(n.val) AS val
FROM sentence AS s
JOIN name AS n ON s.title LIKE CONCAT('%', n.val, '%');

Upvotes: 1

Related Questions