I would like to summarize several other experiments on high-availability clusters. Let's look back at the content summarized by others. It's very fine, and a lot of content has been extended. Sorry, I just skipped it, haha ~~~
Here we will summarize the mysql query statements:
This section describes simple database operations. Next, we will start with the details to introduce mysql query statements;
Import a jiaowu database here to perform the following operations:
Let's take a look at the contents of this database.
- Mysql> show databases;
- + -------------------- +
- | Database |
- + -------------------- +
- | Information_schema |
- | Jiaowu |
- | Mysql |
- | Test |
- + -------------------- +
- 4 rows in set (0.00 sec)
L> use jiaowu;
Database changed
- Mysql> show tables;
- + ------------------ +
- | Tables_in_jiaowu |
- + ------------------ +
- | Courses |
- | Scores |
- | Students |
- | Tutors |
- + ------------------ +
- 4 rows in set (0.00 sec)
- Mysql> select * from students;
- + ----- + -------------- + ------ + -------- + ------ + --------------------- +
- | SID | Name | Age | Gender | CID1 | CID2 | TID | CreateTime |
- + ----- + -------------- + ------ + -------- + ------ + --------------------- +
- | 1 | GuoJing | 19 | M | 2 | 7 | 3 | 10:00:00 |
- | 2 | YangGuo | 17 | M | 2 | 3 | 1 | 10:00:00 |
- | 3 | DingDian | 25 | M | 6 | 1 | 7 | 10:00:00 |
- | 4 | HuFei | 31 | M | 8 | 10 | 5 | 10:00:00 |
- | 5 | HuangRong | 16 | F | 5 | 9 | 9 | 10:00:00 |
- | 6 | YueLingshang | 18 | F | 8 | 4 | NULL | 10:00:00 |
- | 7 | ZhangWuji | 20 | M | 1 | 7 | NULL | 10:00:00 |
- | 8 | Xuzhu | 26 | M | 2 | 4 | NULL | 10:00:00 |
- | 9 | LingHuchong | 22 | M | 11 | NULL | 10:00:00 |
- | 10 | YiLin | 19 | F | 18 | NULL | 10:00:00 |
- + ----- + -------------- + ------ + -------- + ------ + --------------------- +
- 10 rows in set (0.00 sec)
- Mysql> select * from tutors;
- + ----- + -------------- + -------- + ------ +
- | TID | Tname | Gender | Age |
- + ----- + -------------- + -------- + ------ +
- | 1 2 | HuangYaoshi | M | 63 |
- | 3 | Miejueshitai | F | 72 |
- | 4 | OuYangfeng | M | 76 |
- | 5 | YiDeng | M | 90 |
- | 6 | YuCanghai | M | 56 |
- | 7 | Jinlunfawang | M | 67 |
- | 8 | HuYidao | M | 42 |
- | 9 | NingZhongze | F | 49 |
- + ----- + -------------- + -------- + ------ +
- 9 rows in set (0.00 sec)
This is the data used in the following example. You can refer to it first;
The first is the mysql query statement:
Query category:
Single Table query: simple query
Multi-Table query: join query
Subquery: complex Query
Joint Query
Select statement:
Common functions:
# Field -- indicates a field
Total number of count (*) rows
- Mysql> select count (*) from tutors;
- + ---------- +
- | Count (*) |
- + ---------- +
- | 9 |
- + --------- +
- 1 row in set (0.00 sec)
Max (field) returns the maximum value.
- Mysql> select max (age) from tutors;
- + ---------- +
- | Max (age) |
- + ---------- +
- | 93 |
- + ---------- +
- 1 row in set (0.00 sec)
Min (field) returns the minimum value.
Avg (field) Average
- Mysql> select avg (age) from tutors;
- + ---------- +
- | Avg (age) |
- + ---------- +
- | 1, 67.5556 |
- + ---------- +
- 1 row in set (0.00 sec)
Sum () and
- Mysql> select sum (1 + 2 );
- + ---------- +
- | Sum (1 + 2) |
- + ---------- +
- | 3 |
- + ---------- +
- 1 row in set (0.01 sec)
Select selects columns, where selects rows, and combines them to display the attributes of an object as a whole.
The condition is specified after where:
The following conditions can be specified:
Arithmetic comparison:
>,<, =,!,> =, <=, <=> (The result is null and no error occurs)
- Mysql> select name, age from students where age> = 20;
- + ------------- + ------ +
- | Name | age |
- + ------------- + ------ +
- | DingDian | 25 |
- | HuFei | 31 |
- | ZhangWuji | 20 |
- | Xuzhu | 26 |
- | LingHuchong | 22 |
- + ------------- + ------ +
- 5 rows in set (0.00 sec)
Logical comparison of combinations:
And
Or
Not (!)
- Mysql> select name, age from students where! (Age <= 25 );
- + ------- + ------ +
- | Name | age |
- + ------- + ------ +
- | HuFei | 31 |
- | Xuzhu | 26 |
- + ------- + ------ +
- 2 rows in set (0.00 sec)
Comparison of other conditions:
Beween ...... And ......
- Mysql> select name, age from students where age between 24 and 30
- + ---------- + ------ +
- | Name | age |
- + ---------- + ------ +
- | DingDian | 25 |
- | Xuzhu | 26 |
- + ---------- + ------ +
- 2 rows in set (0.00 sec)
The in query field is in the specified list.
- Mysql> select name, age from students where age in (18, 20, 25 );
- + -------------- + ------ +
- | Name | age |
- + -------------- + ------ +
- | DingDian | 25 |
- | YueLingshang | 18 |
- | ZhangWuji | 20 |
- + -------------- + ------ +
- 3 rows in set (0.01 sec)
Is null: the query is null.
- Mysql> select name from students where cid2 is null;
- + ------------- +
- | Name |
- + ------------- +
- | LingHuchong |
- | YiLin |
- + ------------- +
- 2 rows in set (0.00 sec)
Is not null
Like: wildcard matching
%: Match any character of any length
_: Match a single character
Regexp | rlike: Regular Expression matching
Order by: sort. The default value is asc in ascending order.
Desc: Descending Order
- Mysql> select name, age from students where age in (22, 18, 25) order by age desc;
- + -------------- + ------ +
- | Name | age |
- + -------------- + ------ +
- | DingDian | 25 |
- | LingHuchong | 22 |
- | YueLingshang | 18 |
- + -------------- + ------ +
- 3 rows in set (0.00 sec)
Distinct: the uniqueness of the display result, which is attached to the select statement (as described in the following example, only the same cid1 is displayed once)
- Mysql> select distinct cid1 from students order by cid1 desc;
- + ------ +
- | Cid1 |
- + ------ +
- | 18 |
- | 11 |
- | 8 |
- | 6 |
- | 5 |
- | 2 |
- | 1 |
- + ------ +
- 7 rows in set (0.00 sec)
Group by: groups the obtained results. Generally, the group results are used for aggregation operations.
Having: filter the results of a group based on conditions.
- Mysql> select avg (age), cid1 from students group by cid1;
- + ---------- + ------ +
- | Avg (age) | cid1 |
- + ---------- + ------ +
- | 1 | 20.0000 |
- | 20.6667 | 2 |
- | 16.0000 | 5 |
- | 25.0000 | 6 |
- | 1, 24.5000 | 8 |
- | 22.0000 | 11 |
- | 1, 19.0000 | 18 |
- + ---------- + ------ +
- 7 rows in set (0.00 sec)
Limit: limit the number of lines displayed
Eg: limit 1, 2; indicates skipping the first line and then displaying the second line
- Mysql> select avg (age), cid1 from students group by cid1 limit 1, 2;
- + ---------- + ------ +
- | Avg (age) | cid1 |
- + ---------- + ------ +
- | 20.6667 | 2 |
- | 16.0000 | 5 |
- + ---------- + ------ +
- 2 rows in set (0.00 sec)