Introduce common functions of mysql

The mysql video tutorial section introduces the common functions of mysql.

Related free learning recommendations: mysql video tutorial

1. Basic part
1. Use MySQL
1.1, SELECT statement
1.2, sort search data
1.3, filter data
1.4, data filter
1.5, use wildcards to filter
1.6, use regular expressions to search
1.6.1 basic character matching
1.6.2 perform OR match
1.6.3 match one of several characters
1.6.4 matching range
1.6.5 matches special characters
1.6.6 matches the character class
1.6.7 match multiple instances
1.6.8 locators
1.7, calculated fields create
a using MySQL
connected to MySQL will not be opened by the database, so the first thing to do is to open a database:
USE user
USE is followed by the database you want to open. If you don’t know the name of the database, use SHOW DATABASES to view, use SHOW TABLES to view a database Of course, you can also view the column SHOW user_id FROM user in the table. It returns a row for each field. The row contains the field name, data type, whether NULL is allowed, key information, default value and other information (DESCRIBE user is above Statement shortcut)

1.1. SELECT statement
In order to use SELECT to retrieve table data, at least two pieces of information must be given-what you want to choose and where to choose.
The required column name is given after the SELECT keyword, and the FROM keyword indicates the name of the table from which the data is retrieved.
To retrieve multiple columns from a table, use the same SELECT statement. The only difference is that multiple column names must be given after the SELECT keyword, and the column names must be separated by commas.
Use the DISTINCT keyword to retrieve only rows with different values, and the duplicates will not be displayed again (Note: The DISTINCT keyword is applied to all columns and not only the columns that precede it). The
SELECT statement returns all matching rows. They may be specified Each row in the table. In order to return the first row or the first few rows, the LIMIT clause can be used.
LIMIT 5 ,5 instructs MySQL to return 5 rows starting from row 5. The first number is the starting position, and the second number is the number of rows to be retrieved.

1.2. Sorting and retrieving data
In order to sort the data retrieved by the SELECT statement clearly, the ORDER BY clause can be used. The ORDER BY clause takes one or more column names and sorts the output accordingly.
In order to sort by multiple columns, just specify the column name and separate the column names with commas.
Data sorting is not limited to ascending sorting (from A to Z). This is just the default sort order. You can also use the ORDER BY clause to sort in descending order (from Z to A). In order to sort in descending order, the DESC keyword must be specified. (The DESC keyword only applies to the column name directly before it)

1.3. Filtering data
This chapter will teach you how to use the WHERE clause of the SELECT statement to specify search conditions. In the SELECT statement, the data is filtered according to the search criteria specified in the WHERE clause. The WHERE clause is given after the table name (FROM clause). When using ORDER BY and WHERE clauses at the same time, ORDER BY should be placed after WHERE. The WHERE clause operator is shown in the following figure:

1.4. Data Filtering
This chapter teaches how to combine WHERE clauses to create more powerful and advanced search conditions. We will also learn how to use the NOT and IN operators.
AND operator calculation order is higher than OR operator

1.5. Filtering with wildcards
This chapter introduces what wildcards are, how to use wildcards, and how to use the LIKE operator to perform wildcard searches in order to perform complex filtering on data.
In order to use wildcards in the search clause, the LIKE operator must be used. LIKE instructs MySQL that the following search pattern uses wildcard matching instead of direct equal matching for comparison.
The most commonly used wildcard is the percent sign (%). In the search string,% means any character appears any number of times. (According to how MySQL is configured, the search can be case sensitive. If it is case sensitive).
Another useful wildcard is the underscore (_). The underscore has the same purpose as %, but the underscore only matches a single character instead of multiple characters.
As you can see, MySQL wildcards are very useful. But this feature comes at a cost: the processing of wildcard searches generally takes longer than the other searches discussed earlier. Here are some tips to remember when using wildcards.
1. Don't overuse wildcards. If other operators can achieve the same purpose, other operators should be used.
2. When you really need to use wildcards, unless absolutely necessary, don't use them at the beginning of the search pattern. Put the wildcard at the beginning of the search pattern, the search is the slowest.
3. Pay careful attention to the location of wildcards. If it is misplaced, the desired data may not be returned.

1.6. Searching with regular expressions
1.6.1 Basic character matching
Let's start with a very simple example. The following statement retrieves all rows where the column prod_name contains the text 1000:
SELECT prod_name
FROM products
WHERE prod_name REGEXP '1000'
ORDER BY prod_name;

It tells MySQL: What follows REGEXP is treated as a regular expression (a regular expression that matches 1000 in the text body).

SELECT prod_name
FROM products
WHERE prod_name REGEXP ‘.000’
ORDER BY prod_name;

. Is a special character in regular expression language. It means to match any character, therefore, both 1000 and 2000 are matched and returned.
Regular expression matching in MySQL (since version 3.23.4) is not case sensitive (that is, both uppercase and lowercase match). To distinguish between upper and lower case, you can use the BINARY keyword, such as WHERE prod_name REGEXP BINARY'JetPack .000'.

1.6.2 Perform OR matching
To search for one of the two strings (either this string or the other string), use |, as shown below:
SELECT prod_name
FROM products
WHERE prod_name REGEXP '1000|2000'
ORDER BY prod_name;

1.6.3 Matching one of several characters
What if you only want to match certain characters? This can be done by specifying a set of characters enclosed in [and], as shown below:
SELECT prod_name
FROM products
WHERE prod_name REGEXP'[123] Ton'
ORDER BY prod_name;
character sets can also be negated, that is, they will match except Anything other than the specified character. To negate a character set, put a ^ at the beginning of the set. Therefore, although [123] matches characters 1, 2, or 3, [^123] matches anything but these characters.

1.6.4 匹配范围
SELECT prod_name
FROM products
WHERE prod_name REGEXP ‘[1-5] Ton’
ORDER BY prod_name;

1.6.5 Matching special characters The
regular expression language consists of special characters with specific meanings. We have already seen., [], | And-and so on, as well as some other characters. Excuse me, if you need to match these characters, what should you do? For example, if you want to find out the value that contains the. Character, how do you search? In order to match special characters, \ must be used as the leading. \- means search-, \. means search..

SELECT vend_nameFROM vendorsWHERE vend_name REGEXP '\\.'ORDER BY vend_name;

All characters with special meaning in regular expressions must be escaped in this way. This includes., |, [] And other special characters used so far.

1.6.6 Matching character classes
There is a match to find out the numbers, all alphabetic characters, or all the alphabetic characters that you use frequently. For easier work, you can use a predefined character set called a character class.

1.6.7 Matching multiple instances
All the regular expressions used so far have attempted to match a single occurrence. If there is a match, the row is retrieved, if it does not exist, no row is retrieved. But sometimes you need to have stronger control over the number of matches. For example, you might need to find all numbers, no matter how many digits are in the number, or you might want to find a word and also be able to fit a trailing s (if it exists), etc.

SELECT prod_nameFROM productsWHERE prod_name REGEXP'\([0-9] sticks?\)' ORDER BY prod_name; The regular expression \([0-9]sticks?\) needs explanation. \( matches (, [0-9] matches any number (in this example, 1 and 5), sticks? matches sticks and sticks (the? After s makes s optional, because? Matches any character before it 0 times Or 1 occurrence), \) matches). Without?, it would be very difficult to match sticks and sticks.

The following is another example. This time we intend to match the 4 digits connected together:

SELECT prod_nameFROM productsWHERE prod_name REGEXP '[[:digit:]]{4}'ORDER BY prod_name;

1.6.8 Locator
All the examples so far match text anywhere in a string. In order to match the text at a specific location, you need to use the locators listed in the table below.

For example, what if you want to find all products that start with a number (including numbers that start with a decimal point)? A simple search for [0-9.] (or [[:digit:]. ]) will not work, because it will find a match anywhere in the text. The solution is to use the ^ locator, as shown below:

SELECT prod_nameFROM productsWHERE prod_name REGEXP '^[0-9\\.]'ORDER BY prod_name;

1.7. Create a calculated field The
data stored in the table is not what the application needs. We need to retrieve the converted, calculated, or formatted data directly from the database; instead of retrieving the data and then reformatting it in the client application or reporting program. This is where the calculated field comes into play. Unlike the columns introduced in the previous chapters, calculated fields do not actually exist in the database table. The calculated field is created in the SELECT statement at runtime.
In the MySQL SELECT statement, you can use the Concat() function to concatenate two columns.

SELECT Concat(vend_name, ' (', vend_country, ')')FROM vendorsORDER BY vend_name;

Organize the data by deleting the extra spaces on the right side of the data. This can be done using MySQL's RTrim() function, as shown below:

SELECT Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')')FROM vendorsORDER BY vend_name;

An alias is an alternative name for a field or value. The alias is given with the AS keyword. Consider the following SELECT statement:

SELECT Concat(RTrim(vend_name), ' (', RTrim(vend_country), ')') ASvend_titleFROM vendorsORDER BY vend_name;

This article is from php Chinese website; mysql video tutorial column https://www.php.cn/course/list/51.html

Guess you like

Origin blog.csdn.net/Anna_xuan/article/details/110442481
Recommended