MySQL正则表达式初步_MySQL
正则表达式
我们知道,在SQL之中,可以用 like 这个谓词(表达式) 来进行模糊检索,并支持 %,?,_等占位符.但是,这个模糊检索的功能有很多限制,简单来说就是太模糊了。
在MySQL中提供了 REGEXP 关键字来支持正则表达式,当然,只是一些很简单的正则啦。
首先,我们构造一些测试数据。
-- 建表USE test;DROP TABLE IF EXISTS t_regcustomer;CREATE TABLE t_regcustomer ( id INT(10) AUTO_INCREMENT ,name VARCHAR(256) ,age INT(10) , PRIMARY KEY(id)) COLLATE='utf8_general_ci' ENGINE=InnoDB;
-- 插入一些测试数据:TRUNCATE TABLE t_regcustomer;INSERT INTO t_regcustomer(name, age) VALUES ('王明',20);INSERT INTO t_regcustomer(name, age) VALUES ('王大',21);INSERT INTO t_regcustomer(name, age) VALUES ('小王',22);INSERT INTO t_regcustomer(name, age) VALUES ('小王2',22);INSERT INTO t_regcustomer(name, age) VALUES ('敲不死',23);INSERT INTO t_regcustomer(name, age) VALUES ('憨憨',24);INSERT INTO t_regcustomer(name, age) VALUES ('憨憨2',24);INSERT INTO t_regcustomer(name, age) VALUES ('郭靖名',25);INSERT INTO t_regcustomer(name, age) VALUES ('郭靖2',25);INSERT INTO t_regcustomer(name, age) VALUES ('郭靖3',25);INSERT INTO t_regcustomer(name, age) VALUES ('郭得缸',25),('大鹏',20),('大鹏2',20),('大鹏3',20),('二鹏',19),('鹏鹏',18),('鹏鹏1',18),('小鹏',17),('AAA',17),('aaa',17),('SS',17),('s2',17),('ss',17);
1. 最简单的查询:
SELECT *FROM t_regcustomer;
SELECT c.id, c.name, c.ageFROM t_regcustomer c;
SELECT c.id, c.name, c.ageFROM t_regcustomer cORDER BY c.age ASC;
%匹配任意数量(0~n)的任意字符
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name LIKE '%鹏%'ORDER BY c.age ASC;
.匹配任意一个字符
注意此处因为没有起始(^)和结束($)限定符,所以只要列中出现的行都会被检索出来.
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name REGEXP '.鹏.'ORDER BY c.age ASC;
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name REGEXP '^王'ORDER BY c.age ASC;
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name REGEXP BINARY '^s'ORDER BY c.age ASC;
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name REGEXP BINARY 'a|s'ORDER BY c.name ASC;
[123] 表示 1、2、3这3个数字之一出现即可
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name REGEXP BINARY '鹏[123]'ORDER BY c.name ASC;
SELECT c.id, c.name, c.ageFROM t_regcustomer cWHERE c.name REGEXP BINARY '鹏[1-9]'ORDER BY c.name ASC;
使用 //
可以转义 /.[]()?-| 以及分页,换行符号等
11.更多内容
请查阅 《MySQL必知必会》 68页 正则表达式

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Python, as a high-level programming language, is easy to learn and use. Once you need to write a Python program, you will inevitably encounter syntax errors, and expression syntax errors are a common one. In this article, we will discuss how to resolve expression syntax errors in Python. Expression syntax errors are one of the most common errors in Python, and they are usually caused by incorrect usage of syntax or missing necessary components. In Python, expressions usually consist of numbers, strings, variables, and operators. most common

In C or C++, the comma "," has different uses. Here we will learn how to use them. Commas as operators. The comma operator is a binary operator that evaluates the first operand, discards the result, then evaluates the second operand and returns the value. The comma operator has the lowest precedence in C or C++. Example #include<stdio.h>intmain(){ intx=(50,60); inty=(func1(),func2());} Here 60 will be assigned to x. For the next statement, func1( will be executed first

Introduction to how to write exponential function expressions in C language and code examples What is an exponential function? The exponential function is a common type of function in mathematics. It can be expressed in the form of f(x)=a^x, where a is the base and x is the exponent. . Exponential functions are mainly used to describe exponential growth or exponential decay. Code example of exponential function In C language, we can use the pow() function in the math library to calculate the exponential function. The following is a sample program: #include

Lambda expressions in Java With the release of Java 8, lambda expressions have become one of the most concerned and discussed topics among Java developers. Lambda expressions can simplify Java programmers' tedious writing methods, and can also improve the readability and maintainability of programs. In this article, we will take a deep dive into lambda expressions in Java and how they provide a simpler and more intuitive programming experience in Java code.

A lambda expression is an anonymous function that can be conveniently used to iterate over a collection. In this article, we will introduce how to use lambda expressions to iterate over collections, and provide specific code examples. In Python, the syntax format of a lambda expression is as follows: lambda parameter list: The parameter list of an expression lambda expression can contain one or more parameters, separated by commas. The expression is the return value of the lambda function. Let's look at a simple example below, assuming

Introduction and basic syntax of Lambda expressions Lambda expressions consist of a function parameter list, a colon and a function body. The function parameter list is the same as that of an ordinary function, and the function body is an expression rather than a set of statements. #Example: Return a function that receives two numbers and returns their sum sum=lambdax,y:x+y Application scenarios of Lambda expressions Lambda expressions are very suitable for use as callback functions, filter functions and mapping functions. Callback function: A callback function is a function called within another function. Lambda expressions make it easy to create callback functions without declaring their names. Filter function: The filter function is used to filter out full

Lambda expression, as the name suggests, is an anonymous function with the arrow symbol (->) as its core. It allows you to pass blocks of code as arguments to other methods, or store them into variables for later use. Lambda expression syntax is concise and easy to understand, and it is very suitable for processing data flow and parallel computing. 1. The basic syntax of Lambda expression The basic syntax of Lambda expression is as follows: (parameter list)->{code block} Among them, the parameter list and code block are optional. If there is only one parameter, the parentheses can be omitted. If the code block is only one line, the curly braces can be omitted. For example, the following code block uses a Lambda expression to add 1 to a number: List

With the rapid development of computer technology, programming languages are constantly being upgraded and improved. Among them, PHP, as a commonly used web development language, is constantly innovating and launching new versions. Recently, the release of PHP8.0 version has attracted widespread attention. Among them, the improvements to the exception handling mechanism in the new version have attracted a lot of attention. This article will focus on the topic of expression support in the try statement block in PHP8.0. 1. Improvements in the exception handling mechanism of PHP8.0 In previous versions, P
