zoukankan      html  css  js  c++  java
  • MySQL Regexps

     

    You have seen MySQL pattern matching with LIKE ...%. MySQL supports another type of pattern matching operation based on regular expressions and the REGEXP operator. If you are aware of PHP or PERL, then it's very simple for you to understand because this matching is very similar to those scripting regular expressions.

    Following is the table of pattern, which can be used along with REGEXP operator.

    Pattern	What the pattern matches
    ^	Beginning of string
    $	End of string
    .	Any single character
    [...]	Any character listed between the square brackets
    [^...]	Any character not listed between the square brackets
    p1|p2|p3	Alternation; matches any of the patterns p1, p2, or p3
    *	Zero or more instances of preceding element
    +	One or more instances of preceding element
    {n}	n instances of preceding element
    {m,n}	m through n instances of preceding element
    

      

    Examples:

    Now based on above table, you can device various type of SQL queries to meet your requirements. Here, I'm listing few for your understanding. Consider we have a table called person_tbl and it's having a field called name:

    Query to find all the names starting with 'st'

    mysql> SELECT name FROM person_tbl WHERE name REGEXP '^st';
    

      

    Query to find all the names ending with 'ok'

    mysql> SELECT name FROM person_tbl WHERE name REGEXP 'ok$';
    

      

    Query to find all the names, which contain 'mar'

    mysql> SELECT name FROM person_tbl WHERE name REGEXP 'mar';
    

      

    Query to find all the names starting with a vowel and ending with 'ok'

    mysql> SELECT name FROM person_tbl WHERE name REGEXP '^[aeiou]|ok$';
    

      

  • 相关阅读:
    整理:分页存储过程整理
    净利润-流通市值比率”与公司估值
    常见7种股票底部形态(图解)
    nginx
    移动成本分布1
    浅谈公开信息检索和判断能力
    股票技术分析 成交量与换手率专题
    成份股和成份股指数
    股票底部形态初探
    筹码拉抬派发法
  • 原文地址:https://www.cnblogs.com/hephec/p/4570537.html
Copyright © 2011-2022 走看看