Summary of mysql fuzzy query like and regexp

Source: Internet
Author: User
In mysql, like and regexp are implemented for Fuzzy queries. ------------------------ Many people know the usage of like. The most common case is select * fromawherenamelike % a %. % indicates any character, and its effect is like * in a regular expression *, it has several usage methods: a %, % a %, %,

In mysql, like and regexp are implemented for Fuzzy queries. ------------------------ Many people know the usage of like. The most common case is that select * from a where name like '% a %'. '%' indicates any character, it works like '*' in a regular expression. It has several usage methods: 'A % ',' % a % ',' % ',

In mysql, like and regexp are implemented for Fuzzy queries.

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

Many people know the usage of like. The most common case is select * from a where name like '% a % ';

'%' Indicates any character, which is like '*' in a regular expression. It has several usage methods: 'A % ',' % a % ', '% A', which respectively indicates the start, existence, and end of the start.

You can also use the '_' character, which represents any character. The effect is similar to '.' in a regular expression '.'.

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

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

Regexp is the regular expression matching operator of mysql.

The regexp usage is as follows: select * from a where name regexp 'A'. You can refer to the regular expression for its syntax. like '*', '[a-z]', 'a | B ', 'CC $', '^ B {2} $ '.

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

Regexp is not directly used in hibernate, but it can be implemented by adding a custom function.

public class MySQL5DialectRegexp extends org.hibernate.dialect.MySQLInnoDBDialect{  public MySQL5DialectRegexp() {    super();    registerFunction( "regexp", new SQLFunctionTemplate(Hibernate.BOOLEAN, "?1 REGEXP ?2") );  }}


Here, org. hibernate. dialect. MySQLInnoDBDialect is the value of hibernate. dialect in hibernate configuration information. You can use the path of MySQL5DialectRegexp 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.