Mysql string Field 2 ways to determine whether a string is contained _mysql

Source: Internet
Author: User

Suppose you have a table:

Copy Code code as follows:

CREATE TABLE users (id int (6) Not NULL auto_increment,primary KEY (ID), user_name VARCHAR (m) not null,emails VARCHAR (x) T NULL);

Initialize the table and add some records.

Copy Code code as follows:

TRUNCATE TABLE Users
INSERT into users (user_name, emails) VALUES (' Xiao Zhang ', ' a@email.com,b@email.com,c@email.com ');
INSERT into users (user_name, emails) VALUES (' Xiao Wang ', ' aa@email.com,bb@email.com,cc@email.com ');
  

Some of the fields in Mysql are string types, how do you find records that contain certain characters?

Method One:

Copy Code code as follows:

SELECT * from the users WHERE emails like "%b@email.com%";

So bb@email.com users also found out, not in line with expectations.

Method Two:

Using MySQL string function find_in_set ();

Copy Code code as follows:

SELECT * from Users WHERE find_in_set (' aa@email.com ', emails);
  

This is possible, how to understand it?

MySQL has a lot of string functions the Find_in_set (STR1,STR2) function is the index of the location where STR1 is returned in str2, and str2 must be split by "."

e.g.

Copy Code code as follows:

MySQL > SELECT find_in_set () (' B ', ' a,a,b,c,d ') as test;
-> 3

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.