-
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