Search with comma-separated value mysql
i have 2 tables customers
and imap_emails
. customer
table contain emails of customers.
I am using PHP-IMAP to fetch email from email server and then saving to database table imap_emails
.
the imap_emails
table has 2 fields from
and to
and to
field contains comma separated values.
i need to fetch emails from first table and then search against to
and from
on imap_emails
.
First i thought about a LIKE
condition to search , but i would like to have something like FIND_IN_SET
or something other.
How can i achieve this in a better way ? ( For some reasons i cannot use relations on this table )
Please advice.
Example of doing a join based on FIND_IN_SET
SELECT *
FROM imap_emails
INNER JOIN customers
ON FIND_IN_SET(customers.email, imap_emails.to) > 0
You don't say how you want to narrow this down (ie, if an email uses 2 email addresses do you want both emails brought back), so can't add much at this stage.