Mysql fuzzy query like and regexp summary _mysql

Source: Internet
Author: User

The implementation of fuzzy query in MySQL has like and regexp.

------------------------

The use of like is known to many people, and the most common case is select * from a where name like '%a% ';

Where '% ' represents any character, its effect is like ' * ' in a regular expression, it has several uses: ' a% ', '%a% ', '%a ', which means the beginning of what, what is, and what ends.

You can also use the ' _ ' character, which represents an arbitrary character. The effect is similar to the '. ' Inside the regular expression.

Like is a comparison of all the strings in this field, the efficiency is not high.

------------------------

RegExp is the regular matching operator for MySQL.

RegExp uses such as: SELECT * from a WHERE name regexp ' a '; it can be written in reference to regular expressions. Like ' * ', ' [A-z] ', ' a|b ', ' cc$ ', ' ^b{2}$ '.

------------------------

The direct use of regexp is not available in Hibernate, but can be implemented by adding custom functions.

public class Mysql5dialectregexp extends org.hibernate.dialect.mysqlinnodbdialect{public
  mysql5dialectregexp () {
    super ();
    Registerfunction ("RegExp", New Sqlfunctiontemplate (Hibernate.boolean, "1 regexp? 2")
  }


Where Org.hibernate.dialect.MySQLInnoDBDialect is the value of the Hibernate configuration information Hibernate.dialect, then the MYSQL5DIALECTREGEXP path can be used instead.

HQL statements can be written:

From a where regexp (A, ' a|b|c ') = 1;

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.