3

I would like to check if a string in a certain cell of the database is part of a string in another cell of the same row.

The following example makes it more clear. Let's say that my table looks like this:

+---------+-----------------+
|  Name   |     Couple      |
+---------+-----------------+
| Mark    | Mark & Thomas   |
| Chris   | Thomas & Logan  |
| Patrick | Logan & Patrick |
| Thomas  | Chris & Mark    |
| Logan   | Patrick & Chris |
+---------+-----------------+

Then the result of my query would have to be this:

+---------+-----------------+
|  Name   |     Couple      |
+---------+-----------------+
| Mark    | Mark & Thomas   |
| Patrick | Logan & Patrick |
+---------+-----------------+

Because Mark and Patrick are part of the string in the Couple field.

I gave it a try, but I don't think it's the right way to do this:

Select * from table where Couple like %Name%

Thanks for your answer(s)!

2 Answers 2

7

Your syntax was close, you can use:

Select name, couple
from yt 
where Couple like concat('%', Name, '%');

See SQL Fiddle with Demo

Sign up to request clarification or add additional context in comments.

Comments

4

Think you have the columns the wrong way round, and you need the wild cards in the string you are comparing.

Maybe:-

SELECT * FROM table WHERE Couple LIKE CONCAT('%', Name, '%')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.