MySQL string field 3 ways to determine if a string is included

Source: Internet
Author: User

Method One:

SELECT * from the users WHERE emails like "%[email protected]%";

Method Two:

Use the MySQL string function find_in_set ();

SELECT * from Users WHERE find_in_set (' [email protected] ', emails);

This is possible, how to understand it?

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

Note: When STR2 is NO1: "3,6,13,24,33,36", NO2: "13,33,36,39", determine whether the str2 field in two data contains ' 3 ', the function can be perfectly solved

MySQL > SELECT find_in_set (' 3 ', ' 3,6,13,24,33,36 ') as test;
1

MySQL > SELECT find_in_set (' 3 ', ' 13,33,36,39 ') as test;
0

Method Three:

Use the locate (SUBSTR,STR) function, if included, returns the number of >0, otherwise returns 0

Example: Determine if the URL in the site table contains the ' http://' substring, and if it is not included, stitch at the beginning of the URL string
Update site set URL =concat ('/http ', URL) where locate ('/HTTP ', URL) =0

Note that the concatenation of strings in MySQL cannot use the Plus +, with the concat function

MySQL string field 3 ways to determine if a string is included

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.