In the blacklist table of MySQL there is an email field of type VARCHAR (255). It can be written email-addresses, one or more, separated by commas. Task: check a specific email-address for its presence in the blacklist table.
Suppose we need to check email: test@mail.ru for its presence in the table: Option: SELECT id FROM blacklist WHERE email LIKE '%test@mail.ru%' - will not work, because an address can be entered in the table field, for example 123test@mail.ru - which will falsely work for this request!
Since the email field can contain one or several email addresses separated by commas, the following search may be formulated verbally:
Find the exact entry of the string in the field, taking into account such variants of its location in the field, for example test@mail.ru:
- field start test@mail.ru field end (one email in the field)
- beginning of the field test@mail.ru comma or end of the field (email is at the beginning of the field)
- comma test@mail.ru comma or field end (email is in the middle or end of the field)
How to write such regular expression in MySQL?