MySQL functions instr, LOCATE, POSITION VS like

Source: Internet
Author: User
Tags mysql functions



Mysql will use the same language method:



SELECT ' column ' from ' table ' where ' condition ' like '%keyword% '



In fact, you can use both the locate and InStr functions instead



SELECT ' column ' from ' table ' where locate(' keyword ', ' condition ') >0
or locate's name position.
SELECT ' column ' from ' table ' where position(' keyword ' in ' condition ')





Or
SELECT ' column ' from ' table ' where instr(' condition ', ' keyword ') >0


The difference between locate, position, and InStr is just the same as the parameter location, and locate more than one parameter of the starting position.



The speed of these three is a little bit faster than using like.



Note for three additional functions:


    • INSTR (str,substr)


Returns the first occurrence of a substring of string str . This is the same as the two-parameter form of locate (), except that the order of the arguments is reversed.



Mysql> SELECT INSTR (' Foobarbar ', ' Bar ');


4



Mysql> SELECT INSTR (' Xbar ', ' foobar ');


0



This function supports multibyte characters and is case-sensitive only if at least one parameter is a binary string.


    • LOCATE (substr,str), LOCATE (substr,str,POS)


The first syntax returns the first occurrence of the string str neutron string substr . The second syntax returns the first occurrence of the string str neutron string substr , starting at Pos. If substr is not in str , the return value is 0.



Mysql> SELECT LOCATE (' Bar ', ' Foobarbar ');


4



Mysql> SELECT LOCATE (' Xbar ', ' foobar ');


0



Mysql> SELECT LOCATE (' Bar ', ' Foobarbar ', 5);


7



This function supports multibyte characters and is case-sensitive only if at least one parameter is a binary string.


    • POSITION (substr in str)


POSITION (substr in str) is a synonym for LOCATE (substr,str).



MySQL functions instr, LOCATE, POSITION VS like


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.