0

If I have a table called 'invitation' like this:

| invitation_id | sender | recipient             |
| 1             | Josh   | James, Hendry, Max    |
| 2             | Steave | James, Gerrard, Ramon |
| 3             | Steave | Gerrard, Max          |

How can I show invitation data that I received, if my name is "James"?

4 Answers 4

2

When using like %james% you may get incorrect results, if similar name occurs. So using find_in_set can solve your problem.

select * from invitation where find_in_set('james',recipient) <> 0

You can also use like in this way.

select * from invitation where ',' + recipient + ',' like '%,james,%'
Sign up to request clarification or add additional context in comments.

Comments

1

Use LIKE:

SELECT *
FROM invitation
WHERE recipient LIKE '%james%'

1 Comment

This will do the job but you will face trouble if there is another recipient called James Bond :)
1

Storing the data (recipient) in comma separated is really not a good practice.

But you still can use find_in_set

You should iterate with you all the values of recipient and do like this

SELECT FIND_IN_SET('James','James,Hendry,Max')

SELECT FIND_IN_SET('James','James, Gerrard, Ramon')

Which will return 0 or 1

Comments

0

Use of LIKE will be good for searching purpose. But We should use the function FIND_IN_SET() for this type of problem, Where data in table exist as comma separated format.

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.