开发者

Help with a MySQL SELECT WHERE Clause

开发者 https://www.devze.com 2023-02-03 17:19 出处:网络
A column in my table contains email addresses. I have a text string that contains the a few usernames of email addresses separated by commas. I can make text sting into an array if necessary to get m

A column in my table contains email addresses.

I have a text string that contains the a few usernames of email addresses separated by commas. I can make text sting into an array if necessary to get my SELECT WHERE clause to work correctly.

Text string search argument is 'bob,sally,steve开发者_开发百科'

I want to produce a WHERE clause that only returns rows where the username portion of the email address in the table matches one of the usernames in my text string search argument.

Thus a row with frank@email.com would not be returned but sally@zmail.com would be.

Does anyone have a WHERE clause sample that produces this result?

Thanks.


Maybe something like

WHERE email like '%bob%' or email like '%sally%' or email like '%steve%'

Note that this will also match adjklqwefh@bob.com. If you only want to match the starting characters, simply omit the leading %:

WHERE email like 'bob%' or email like 'sally%' or email like 'steve%'

If you want to be more restrictive, you could try:

WHERE email like 'bob@%.com' or email like 'sally@%.com' or email like 'steve@%.com'

Also, read up on the LIKE operator, and regular expressions (you shoudln't need it here, but you'll probably need it eventually).


This will work for you but I think it will be very slow

WHERE SUBSTRING(email, 1, (LOCATE('@', email) - 1)) in ('steve', 'smith', .... )


The following statement will select any rows in the 'email' column where the address starts with "bob@". You would would substitute "bob" with the username you are trying to match.

SELECT * FROM table_name WHERE email REGEXP "^bob@"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号