SELECT
Provide data query function
Syntax: SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT] [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS] select_expr [, select_expr ...] [FROM table_references [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [INTO OUTFILE 'file_name' [CHARACTER SET charset_name] export_options | INTO DUMPFILE 'file_name' | INTO var_name [, var_name]] [FOR UPDATE | LOCK IN SHARE MODE]]
1) Query all information in the form
MariaDB [hellodb]> SELECT * FROM students;
2) Query a specific field in the form
MariaDB [hellodb]> SELECT user,host FROM mysql.user;
3) Field display alias function
MariaDB [hellodb]> select user as 名字,host as 主机名 FROM mysql.user;
+--------+-----------+
| 名字 | 主机名 |
+--------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | ansible |
| root | ansible |
| | localhost |
| root | localhost |
+--------+-----------+
4) WHERE filter condition query.
name | Operator |
---|---|
Arithmetic comparison operator | +, -, *, /, % |
Comparison operator | =, !=, <>, <=>, >, >=, <, <= |
Judge null | IS NULL |
Judge not empty | IS NOT NULL |
Which fields are included | IN (element1, element2, …) |
A certain range | BETWEEN min_num AND max_num |
MariaDB [ydong]> SELECT * FROM students WHERE id!=1;
+----+----------+-------+
| id | name | phone |
+----+----------+-------+
| 2 | xiaohong | 456 |
| 3 | xiaobai | NULL |
+----+----------+-------+
2 rows in set (0.02 sec)
MariaDB [ydong]> SELECT * FROM students WHERE id BETWEEN 1 AND 2;
+----+----------+-------+
| id | name | phone |
+----+----------+-------+
| 1 | xiaoming | 123 |
| 2 | xiaohong | 456 |
+----+----------+-------+
2 rows in set (0.00 sec)
MariaDB [ydong]> SELECT * FROM students WHERE phone IS NULL;
+----+---------+-------+
| id | name | phone |
+----+---------+-------+
| 3 | xiaobai | NULL |
+----+---------+-------+
1 row in set (0.00 sec)
MariaDB [ydong]> SELECT * FROM students WHERE name IN ('xiaobai');
+----+---------+-------+
| id | name | phone |
+----+---------+-------+
| 3 | xiaobai | NULL |
+----+---------+-------+
1 row in set (0.01 sec)
5) like, query the fields that meet the conditions defined
- %: any character of any length
- _: any single character
MariaDB [ydong]> SELECT * FROM students WHERE name LIKE '%g%';
+----+----------+-------+
| id | name | phone |
+----+----------+-------+
| 1 | xiaoming | 123 |
| 2 | xiaohong | 456 |
+----+----------+-------+
2 rows in set (0.01 sec)
6) GROUP: According to the specified conditions, the query results are "grouped" for "aggregation" operation
- avg(): find the average
- max(): Maximum
- min(): minimum
- count(): count
- sum(): sum
MariaDB [ydong]> select sex,avg(score) from students group by sex;
+------+------------+
| sex | avg(score) |
+------+------------+
| f | 90 |
| m | 87.5 |
+------+------------+
2 rows in set (0.01 sec)
根据性别取成绩平均值
- HAVING: Specify the filter condition for the result of the grouping aggregation operation, as long as there is a grouping, you must use having
MariaDB [ydong]> SELECT sex,avg(score) from students group by sex having avg(score) > 80 ;
+------+-------------------+
| sex | avg(score) |
+------+-------------------+
| f | 86.66666666666667 |
+------+-------------------+
1 row in set (0.01 sec)
8) ORDER BY: Sort the query results according to the specified field
MariaDB [ydong]> SELECT name,score FROM students ORDER BY score DESC;
+-----------+-------+
| name | score |
+-----------+-------+
| xiaobai | 95 |
| xiaohong | 90 |
| xiaoming | 80 |
| xiaolan | 70 |
| xiaocheng | 60 |
| xiaohuang | 100 |
+-----------+-------+
6 rows in set (0.00 sec)
DESC 顺序排列
MariaDB [ydong]> SELECT name,score FROM students ORDER BY score ASC;
+-----------+-------+
| name | score |
+-----------+-------+
| xiaohuang | 100 |
| xiaocheng | 60 |
| xiaolan | 70 |
| xiaoming | 80 |
| xiaohong | 90 |
| xiaobai | 95 |
+-----------+-------+
6 rows in set (0.02 sec)
逆序排列
MariaDB [ydong]> SELECT name,phone FROM students ORDER BY -phone ASC;
+-----------+-------+
| name | phone |
+-----------+-------+
| xiaobai | NULL |
| xiaohuang | NULL |
| xiaolan | NULL |
| xiaocheng | NULL |
| xiaohong | 456 |
| xiaoming | 123 |
+-----------+-------+
6 rows in set (0.01 sec)
-phone意思是将null值按照逆序ASC还是顺序DESC排列,仅对数字有效
9) LIMIT [[offset,]row_count]: limit the number of output rows of the query results
MariaDB [ydong]> SELECT name,phone FROM students ORDER BY -phone ASC LIMIT 2,4;
+-----------+-------+
| name | phone |
+-----------+-------+
| xiaolan | NULL |
| xiaocheng | NULL |
| xiaohong | 456 |
| xiaoming | 123 |
+-----------+-------+
4 rows in set (0.01 sec)
意思是从第三行往下数4行。
Multi-table query
1) The left join will read all the data of A, even if there is no corresponding data
in table B. We use students as table A and table B as teachers table.
MariaDB [hellodb]> select stu.Name as stu_name ,t.Name as teacher_name from students as stu left join teachers as t on stu.TeacherID=t.TID;
+---------------+---------------+
| stu_name | teacher_name |
+---------------+---------------+
| Shi Zhongyu | Miejue Shitai |
| Shi Potian | NULL |
| Xie Yanke | NULL |
| Ding Dian | Lin Chaoying |
| Yu Yutong | Song Jiang |
...
Inner join, take the intersection of two tables
MariaDB [hellodb]> SELECT stu.name as 学生名字,t.name as 教师名字 FROM students as stu inner join teachers as t on stu.TeacherID=t.TID;
+--------------+---------------+
| 学生名字 | 教师名字 |
+--------------+---------------+
| Yu Yutong | Song Jiang |
| Shi Zhongyu | Miejue Shitai |
| Ding Dian | Lin Chaoying |
+--------------+---------------+
3 rows in set (0.02 sec)
Right connection, opposite to left connection
MariaDB [hellodb]> SELECT t.Name as 教师名字, stu.Name as 学生名字 FROM students as stu right join teachers as t on stu.TeacherID=t.TID;
+---------------+--------------+
| 教师名字 | 学生名字 |
+---------------+--------------+
| Miejue Shitai | Shi Zhongyu |
| Lin Chaoying | Ding Dian |
| Song Jiang | Yu Yutong |
| Zhang Sanfeng | NULL |
+---------------+--------------+
4 rows in set (0.01 sec)
4)
A table and B table are displayed at the same time
Two tables are displayed at the same time using union
MariaDB [hellodb]> SELECT Stuid,Name from students
-> union
-> SELECT Tid,Name from teachers;
5)
Remove the field of A after the intersection of A and B
MariaDB [hellodb]> SELECT stu.Name,t.Name AS teacher_name FROM students AS stu LEFT JOIN teachers AS t on stu.TeacherID=t.TID WHERE t.name is null;
Subquery
In the query statement in the nested query statement, the performance is generally poor
Used for subqueries in where clauses
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Age > (SELECT Avg(Age) FROM students);
+--------------+-----+
| Name | Age |
+--------------+-----+
| Xie Yanke | 53 |
| Ding Dian | 32 |
| Shi Qing | 46 |
| Tian Boguang | 33 |
| Sun Dasheng | 100 |
+--------------+-----+
5 rows in set (0.05 sec)
Subqueries used in IN
MariaDB [hellodb]> SELECT Name,Age FROM students WHERE Age IN (SELECT Age FROM teachers);
Used for subqueries in FROM clause
MariaDB [hellodb]> SELECT * FROM (SELECT ClassID,AVG(Age) AS aage FROM students GROUP BY ClassID HAVING ClassID IS NOT NULL) AS s WHERE s.aage < 30;
+---------+---------+
| ClassID | aage |
+---------+---------+
| 1 | 20.5000 |
| 3 | 20.2500 |
| 4 | 24.7500 |
| 6 | 20.7500 |
| 7 | 19.6667 |
+---------+---------+
5 rows in set (0.00 sec)