MySQL find how to tell if a field contains a string

Source: Internet
Author: User

MySQL find out how to tell if a field contains a string there is such a requirement in MySQL DatabaseIn a String field (permission), the user has several different mailboxes, separated by ', ', and now wants to take out all the member lists of a mailbox. Suppose you have a table:
CREATE TABLE int (6notNULL auto_increment,PRIMARYKEY (ID),user_name  varchar(notnullvarchar );

Initialize the table and add some records.
truncate TableUsersINSERT  intoUsersuser_name, emails)VALUES('Xiao Zhang','[email protected],[email protected],[email protected]');INSERT  intoUsersuser_name, emails)VALUES('Xiao Wang','[email protected],[email protected],[email protected]');

Some of the fields in Mysql are string types, how do you find records that contain some characters? Method One is very simple can be achieved by using% for example
SELECT *  from WHERE  like "%b@email. com%";

But if you look at this, you'll find out [email protected] and it doesn't fit the demand. Method Two utilization MySQLString function Find_in_set ();
SELECT *  from WHERE find_in_set ('[email protected]', emails);

This way you can find the email field contains [email protected] all the users so that we can achieve the desired effect, the problem is solved! Note that the MySQL string function find_in_set (STR1,STR2) function is the index of the location where STR1 is returned in str2, and the str2 must be separated by ",".

MySQL find how to tell if a field contains a string

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.