当前位置:网站首页>MySQL Leak Detection and Filling (2) Sorting and Retrieval, Filtering Data, Fuzzy Query, Regular Expression

MySQL Leak Detection and Filling (2) Sorting and Retrieval, Filtering Data, Fuzzy Query, Regular Expression

2022-08-09 09:21:00 Hu Letian

一、数据表

/*
 Navicat Premium Data Transfer

 Source Server         : hu
 Source Server Type    : MySQL
 Source Server Version : 80018
 Source Host           : localhost:3306
 Source Schema         : uer

 Target Server Type    : MySQL
 Target Server Version : 80018
 File Encoding         : 65001

 Date: 28/02/2021 16:23:07
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for orderby
-- ----------------------------
DROP TABLE IF EXISTS `orderby`;
CREATE TABLE `orderby`  (
  `auto` int(10) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `shor` int(5) NULL DEFAULT NULL,
  PRIMARY KEY (`auto`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 11 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of orderby
-- ----------------------------
INSERT INTO `orderby` VALUES (1, 'hu', 5);
INSERT INTO `orderby` VALUES (2, 'hu', 5);
INSERT INTO `orderby` VALUES (3, 'hu', 3);
INSERT INTO `orderby` VALUES (4, 'hu', 2);
INSERT INTO `orderby` VALUES (5, 'hu', 1);
INSERT INTO `orderby` VALUES (6, 'hu', 5);
INSERT INTO `orderby` VALUES (7, 'hu', 8);
INSERT INTO `orderby` VALUES (8, 'hu', 4);
INSERT INTO `orderby` VALUES (9, 'hu', 2);
INSERT INTO `orderby` VALUES (10, 'hu', 3);

SET FOREIGN_KEY_CHECKS = 1;

二、排序检索数据

The retrieved data is not displayed in a purely random order,如果不排序,数据一般将以它在底层表中出现的顺序显示,

这可以是数据最初添加到表中的顺序,但是,If the data is later updated or deleted,则此顺序将会受到MySQL重用回收存储空间的影响.因此,如果不明确控制的话,不能(也不应该)依赖该排序顺序.

关系数据库设计理论认为,如果不明确规定排序顺序,则不应该假定检索出的数据的顺序有意义.

select * from orderBy order by shor asc,auto

降序:desc

升序:asc

注意:If you want to sort on multiple columns,You need to add a keyword to each column(desc或者asc)

升序是默认排序方式,可以不写

三、过滤数据

数据库表一般包含大量的数据,很少需要检索表中的所有行,Often a subset of table data is extracted based on the needs of a specific operation or report.Only retrieve the required data,Search criteria need to be formulated,Also called filter conditions.

操作符:

= 等于

<> 不等于

!= 不等于

< 小于

<= 小于等于

> 大于

>= 大于等于

Between 在指定的两个值之间

select * from orderBy where short between 3 and 5

空值检查

Null:空值,He is included with the field0、空字符串或仅仅包含空格不同

-- Put a few pieces of data before testingshor字段置为null
select * from orderBy where shor is null

注意:在通过过滤选择出不具有特定值的行时,你可能希望返回具有NULL的行.但是,不行,Because the location has a special meaning,数据库不知道他们是否匹配,So they are not returned when matching filtering or not matching filtering.

四、数据过滤

The previous section covered it allwhere子句在过滤数据时使用的都是单一的条件,为了进行更强的过滤控制,MySQL允许给出多个WHERE子句,These clauses can be combined in two ways:and子句或者or子句.

and和or的优先级问题

SQLwhile the statement is being processed,会优先处理and再去处理or.If you want to deal with it firstor子句,You need to add parentheses in the corresponding position.

IN操作符

in操作符用来指定条件范围,Every condition in the range can be used to match.in取合法值的由逗号分隔的清单,All are enclosed in parentheses.

NOT操作符

where子句中的not操作符有且只有一个功能,That is to negate any condition that follows it.

select * from orderBy where shor not in (5,8)

注意:Mysql支持not对in、between和exists(存在)子句取反

五、使用通配符进行过滤

通配符:Special characters used to match part of a value.

搜索模式:由字面值、通配符或两者组合成的搜索条件.

在搜索子句中使用通配符,必须使用like操作符,like指示Mysql,The search pattern that follows compares using wildcards rather than direct equality matching.

There is a price to using this functionality:Wildcard searches generally take longer to process than the other searches mentioned earlier.

%通配符

%Represents any number of occurrences of any character

注意:根据MySQL的配置方式,搜索是可以区分大小写的,如果区分大小写,则‘hu%’和 HuLeTianwill be mismatched.%不可以匹配NULL.

下划线(_)通配符

_代表匹配单个字符,只能匹配一个,不能多也不能少.

六、正则表达式

The filtering examples in the previous sections allow matching、比较和通配符寻找数据,对于基本的过滤,这样就足够了,But as the complexity of the filter conditions increases,where子句本身的复杂性也有必要增加.

例如:Want to extract phone numbers from a text file、Regular expressions can be used to find all files with numbers in their names, etc.

Regular expressions are built using the Regular Expression Language,The regular expression language is a special language used to do all the work just discussed and more.

正则表达式与MySQL的关系

Regular expressions are used to match text,将一个模式(正则表达式)与一个文本串进行比较,MySQL用where子句对正则表达式提供了初步的支持,Allows you to specify regular expression filteringselect检索出的数据.

如果你熟悉正则表达式,则要注意:MySQL仅支持多数正则表达式实现的一个很小的子集.

基本字符匹配

select shor
from zhengze
where shor regexp '2'
order by shor asc

结果:

| shor   |
|--------|
| 200    |
| 245    |
| 5245   |
| 24050  |
| 47278  |

分析:

regexpIt is followed by a regular expression,This sentence means:查询zhengze表中shorThe text of the column contains characters‘2’的所有行.

等同于 like ‘%2%’

The meaning of characters in regular expressions:

|  字符      |  含义                    |
|  ----     | -------------------------|
| .         | 匹配任意一个字符            |
| [字符]     | Matches any single character in parentheses  |

注意:

MySQL中的正则表达式匹配(自3.23.4后),不区分大小写,即大小写都匹配.

进行OR匹配

shor regexp '2|1'

分析:

该正则表达式表示:匹配shorColumn contains all data1或者包含2的行.

shor regexp '[123] Ton'

分析:

匹配shor列中包含1 Ton或者2 Ton或者3 Ton的所有行.

注意:

[]:Represents matching a specific single character

[123]代表的含义:[1|2|3],i.e. there can only be one character,是1或者2或者3

regexp ‘1|2|3 Ton’:代表匹配1或者2或者3 Ton

匹配范围

[1-3]:表示1或2或3

[a-z]:表示a到z这26个英文字母(不区分大小写)

Match special characters

Suppose we want to match contains’.‘的所有行,regexp ‘.’ ,Writing this will match all lines,因为’.'代表任意一个字符.

为了匹配特殊字符,必须用\\为前导.例如:’\\-’:表示查找’-’,’\\.’:表示查找’.’.This is called escaping(escaping).

为了匹配\本身,则需要使用\\\.

注意:

多数正则表达式实现使用单个反斜杠转义特殊字符,以便能使用这些字符本身.但MySQL要求两个反斜杠(MySQL自己解释一个,The regular expression library interprets the other one).

匹配多个实例

目前为止使用的所有正则表达式都试图匹配单次出现.如果存在一个匹配,该行被检索出来,如果不存在,No rows were retrieved.但有时需要对匹配的数目进行更强的控制.例如:To find all numbers,不管数中包含多少数字.

重复元字符

|  元字符   | 说明            |
|  ----  | -----------------|
|  *     | 0个或者多个匹配    |
|  +     | 1个或者多个匹配    |
|  ?     | 0个或者1个匹配     |
|  {n}   | 指定n个匹配        |
|  {n,}  | 不少于n个匹配      |
|  {n,m} | The number of matches is inn和m之间  |

例如:

regexp ‘\\([0-9] sticks?\\)’

(即为(,[0-9]:表示匹配0-9中的任意一个数字,然后加上stick或者sticks(因为?代表0个s或者一个s),最后在加上).

That is, to match the following substring:

(0 stick) (0 sticks)

(1 stick) (1 sticks)

(2 stick) (2 sticks)

(3 stick) (3 sticks)

(4 stick) (4 sticks)

(5 stick) (5 sticks)

(6 stick) (6 sticks)

(7 stick) (7 sticks)

(8 stick) (8 sticks)

(9 stick) (9 sticks)

定位符

目前为止,All examples are matching text anywhere in a string,为了匹配特定位置的文本,The locators in the table below are required.

^:文本的开始

$:文本的结束

[[:<:]]:词的开始

[[:>:]]:词的结束

注意:

like和regexp的不同在于,like匹配整个串,regexp匹配子串.利用定位符,通过^开始每个表达式,通过$结束每个表达式,可以使regexp和like效果一样.

原网站

版权声明
本文为[Hu Letian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090907014820.html