MySQL leaks and fills vacancies (two) sorting retrieval, filtering data, fuzzy query, regular expression

1. Data Sheet

/*
 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;

Second, sort and retrieve data

The retrieved data is not displayed in a purely random order. If it is not sorted, the data will generally be displayed in the order in which it appears in the underlying table.

This can be the order in which the data was initially added to the table, but if the data is later updated or deleted, this order will be affected by MySQL's reuse of storage space. Therefore, the sort order cannot (and should not) be relied upon if it is not explicitly controlled.

Relational database design theory believes that if the sort order is not clearly specified, it should not be assumed that the order of the retrieved data is meaningful.

select * from orderBy order by shor asc,auto

Descending order: desc

Ascending order: asc

Note: If you want to sort multiple columns, you need to add keywords (desc or asc) to each column

Ascending order is the default sorting method, you can leave it alone

Three, filter data

Database tables generally contain a large amount of data, and it is rarely necessary to retrieve all rows in the table. Usually, a subset of the table data is extracted according to the needs of a specific operation or report. To retrieve only the data you need, you need to formulate search conditions, also called filter conditions.

Operator:

= Equal to

<> is not equal to

!= Not equal

<Less than

<= less than or equal to

> Greater than

>= greater than or equal

Between is between two specified values

select * from orderBy where short between 3 and 5

Null check

Null: Null value, it is different from the field containing 0, empty string or only containing spaces

-- 测试之前将几条数据的shor字段置为null
select * from orderBy where shor is null

Note: When selecting rows that do not have a specific value through filtering, you may want to return rows with NULL. However, no, because the location has a special meaning, the database does not know whether they match, so it will not return them in the match filter or non-match filter.

Four, data filtering

The previous section introduced that all where clauses use a single condition when filtering data. For stronger filtering control, MySQL allows multiple WHERE clauses to be given. These clauses can be used in combination in two ways: and clause or or clause.

The priority of and and or

When the SQL statement is processed, it will be processed first and then processed or. If you want to process the or clause first, you need to add parentheses at the corresponding position.

IN operator

The in operator is used to specify the range of conditions, and each condition in the range can be used for matching. A comma-separated list of legal values ​​for in, all enclosed in parentheses.

NOT operator

The not operator in the where clause has one and only one function, that is, it negates any conditions that follow it.

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

Note: Mysql supports not to negate the in, between and exists clauses

Five, use wildcards to filter

Wildcard: A special character used to match part of a value.

Search mode: search conditions composed of literal values, wildcards, or a combination of both.

To use wildcards in search clauses, you must use the like operator, like indicates Mysql, and the following search pattern uses wildcards instead of direct equal matching for comparison.

There is a price to use this feature: the processing of wildcard searches generally takes longer than the other searches mentioned earlier.

% Wildcard

% Represents any number of occurrences of any character

Note: According to the configuration of MySQL, the search is case-sensitive. If it is case-sensitive,'hu%' and HuLeTian will not match. % Cannot match NULL.

Underscore (_) wildcard

_ Means to match a single character, only one can be matched, no more or no less.

Six, regular expressions

The filtering examples in the previous sections allow matching, comparison, and wildcards to search for data. For basic filtering, this is sufficient, but as the complexity of the filtering conditions increases, the complexity of the where clause itself must also increase.

For example, you can use regular expressions if you want to extract phone numbers from text files, find all files with numbers in the middle, and so on.

Regular expressions are built using regular expression language, which is a special language used to complete all the tasks just discussed and more.

The relationship between regular expressions and MySQL

Regular expressions are used to match text. Compare a pattern (regular expression) with a text string. MySQL provides preliminary support for regular expressions with the where clause, allowing you to specify regular expressions to filter select and retrieve The data.

If you are familiar with regular expressions, be aware: MySQL only supports a small subset of most regular expression implementations.

Basic character matching

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

result:

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

analysis:

Regexp is followed by regular expressions. This sentence means: query all rows where the text of the shor column in the zhengze table contains the character '2'.

Equivalent to like'%2%'

The meaning of characters in regular expressions:

|  字符      |  含义                    |
|  ----     | -------------------------|
| .         | 匹配任意一个字符            |
| [字符]     | 匹配任意一个括号中的单一字符  |

note:

Regular expression matching in MySQL (since 3.23.4) is case-insensitive, that is, both upper and lower case are matched.

OR matching

shor regexp '2|1'

analysis:

This regular expression means: match rows that contain 1 or 2 in all data in the shor column.

shor regexp '[123] Ton'

analysis:

Matches all rows that contain 1 Ton or 2 Ton or 3 Ton in the shor column.

note:

[]: Represents matching a specific single character

[123] stands for meaning: [1|2|3], that is, there can only be one character, which is 1 or 2 or 3

regexp '1|2|3 Ton': Represents matching 1 or 2 or 3 Ton

Match range

[1-3]: means 1 or 2 or 3

[az]: represents the 26 English letters from a to z (not case sensitive)

Match special characters

Suppose we want to match all lines that contain'.', regexp'.', so writing will match all lines, because'.' represents any character.

In order to match special characters, you must use \\ as the leading. For example:'\\-': means to search for'-','\\.': to search for'.'. This is called escaping.

In order to match \ itself, you need to use \\\.

note:

Most regular expression implementations use a single backslash to escape special characters so that the characters themselves can be used. But MySQL requires two backslashes (MySQL interprets one by itself, and the regular expression library interprets the other).

Match multiple instances

All regular expressions used so far try to match a single occurrence. If there is a match, the row is retrieved, if it does not exist, no row can be retrieved. But sometimes it is necessary to have stronger control over the number of matches. For example: to find all numbers, no matter how many numbers are contained in the number.

Repeating metacharacters

|  元字符   | 说明            |
|  ----  | -----------------|
|  *     | 0个或者多个匹配    |
|  +     | 1个或者多个匹配    |
|  ?     | 0个或者1个匹配     |
|  {n}   | 指定n个匹配        |
|  {n,}  | 不少于n个匹配      |
|  {n,m} | 匹配数目在n和m之间  |

E.g:

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

(That is (, [0-9]: means to match any number from 0-9, then add stick or sticks (because? represents 0 s or one s), and add it at the end).

That is to match the following substrings:

(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)

Locator

So far, all the examples are matching the text at any position in a string. In order to match the text at a specific position, you need to use the locators in the following table.

^: the beginning of the text

$: end of text

[[:<:]]: the beginning of the word

[[:>:]]: the end of the word

note:

The difference between like and regexp is that like matches the entire string and regexp matches substrings. Using the locator, start each expression with ^, and end each expression with $, you can make regexp and like have the same effect.

Guess you like

Origin blog.csdn.net/weixin_44613100/article/details/114238337