MySQL is more efficient than the like statement locate position InStr Find_in_set

Source: Internet
Author: User

If you have been looking for a more efficient method than the MySQL like statement, let me introduce you to a few.

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

In fact, you can useLocate (position) and InStrThese two functions take the place of

Locate statements
SELECT ' column ' from ' Table ' where locate (' keyword ', ' condition ') >0

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

Or
InStr Statements
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 the parameter of locate more than one starting position.
mysql> SELECT LOCATE (' Bar ', ' Foobarbar ', 5);

7

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



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Gorgeous split-line ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
and we're going to introduce you to a new member, that's find_in_set.

 find_in_set (STR1,STR2) function: Returns the position index where STR2 is located in str1, where str2 must be separated by ",". Table:mysql> SELECT * FROM region;+----+-------------+| ID |  name                     |+----+-------------+|  1   | name1,nam2   | |  2   | name1                  | |  3   | Name3          | |  4   | name2,name4 | | 5   | name3,name5 |+----+-------------+5 rows in Set (0.00 sec) find_in_set statement MySQL > select * from Test where find_in_set (' name1 ', name);  +----+------------+| ID |  Name          |+----+------------+|  1   | name1,nam2 | | 2 ,  | name1          |+----+------------+2 rows in Set (0.02 sec) Thank you for your attention websites blog. 
     

More efficient writing in MySQL than like statements locate position InStr Find_in_set

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.