2

I have created a mail table where i have the following fields.

id    mail_to    mail_subject    mail_message
1     6,9,10     Test Mail       Test Message
2     4,8,6      Test Mail       Test Message

Values stored in mail_to field are id of users. I want to display all the mail where mail_to is 6. How to use the where condition in this case.

I tried to achieve the result with this query but it did not work.

SELECT * FROM tbl_profile_inbox WHERE mail_to = '6' ;

Can you guys help on this one.

2
  • SUBSTRING may lead towards wrong output in some particular cases like mail_to = 9,66,10 Commented Apr 25, 2016 at 7:44
  • 1
    I would suggest change your table design. Commented Apr 25, 2016 at 7:44

2 Answers 2

3

You can use FIND_IN_SET() for that:

SELECT * FROM tbl_profile_inbox WHERE FIND_IN_SET('6',mail_to)
Sign up to request clarification or add additional context in comments.

2 Comments

This is a nice solution if you don't want to change your table design. But still I would suggest change your table design.
Why do i need to change my table design. Is there anything wrong in this design.
0

you can use REGEXP also.

SELECT * FROM tbl_profile_inbox WHERE mail_ to REGEXP '[[:<:]]6[[:>:]]'

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.