首页 > 数据库 >Mysql之查询语句

Mysql之查询语句

时间:2024-03-09 19:33:05浏览次数:27  
标签:语句 Name mysql Age Mysql 查询 Shi Pai NULL

前言:

Mysql中查询语句是日常使用最频繁和复杂的语句,Mysql查询有单表查询和多表连接查询,以下通过案例来熟悉Mysql的查询语句。

一、单表查询

现有hellodb数据库和students等表

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dbtest             |
| hellodb            |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
| test               |
| testdb             |
+--------------------+
9 rows in set (0.00 sec)

mysql> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

mysql> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes           |
| coc               |
| courses           |
| scores            |
| students          |
| teachers          |
| test              |
| toc               |
+-------------------+
8 rows in set (0.00 sec)

查询Gender字段并去重

mysql> SELECT DISTINCT  Gender FROM students;
+--------+
| Gender |
+--------+
| M      |
| F      |
+--------+
2 rows in set (0.00 sec)

查看缓存命中次数

mysql> SHOW GLOBAL STATUS LIKE 'Qcache%';
+-------------------------+----------+
| Variable_name           | Value    |
+-------------------------+----------+
| Qcache_free_blocks      | 1        |
| Qcache_free_memory      | 1031832  |
| Qcache_hits             | 0        |
| Qcache_inserts          | 0        |
| Qcache_lowmem_prunes    | 0        |
| Qcache_not_cached       | 34450935 |
| Qcache_queries_in_cache | 0        |
| Qcache_total_blocks     | 1        |
+-------------------------+----------+
8 rows in set (0.00 sec)

查看查询的次数

mysql> SHOW GLOBAL STATUS LIKE 'Com_se%';
+----------------+----------+
| Variable_name  | Value    |
+----------------+----------+
| Com_select     | 34450938 |
| Com_set_option | 53743714 |
+----------------+----------+
2 rows in set (0.01 sec)

命中率=(命中总次数/查询总次数)*100

查询字段使用别名

mysql> SELECT Name  FROM students;
+---------------+
| Name          |
+---------------+
| Diao Chan     |
| Ding Dian     |
| Duan Yu       |
| Hua Rong      |
| Huang Yueying |
| Jinjiao King  |
| Lin Chong     |
| Lin Daiyu     |
| Lu Wushuang   |
| Ma Chao       |
| Ren Yingying  |
| Shi Potian    |
| Shi Qing      |
| Shi Zhongyu   |
| Sun Dasheng   |
| Tian Boguang  |
| Wen Qingqing  |
| Xi Ren        |
| Xiao Qiao     |
| Xie Yanke     |
| Xu Xian       |
| Xu Zhu        |
| Xue Baochai   |
| Yinjiao King  |
| Yu Yutong     |
| Yuan Chengzhi |
| Yue Lingshan  |
+---------------+
27 rows in set (0.00 sec)

mysql> SELECT Name AS StuName FROM students;
+---------------+
| StuName       |
+---------------+
| Diao Chan     |
| Ding Dian     |
| Duan Yu       |
| Hua Rong      |
| Huang Yueying |
| Jinjiao King  |
| Lin Chong     |
| Lin Daiyu     |
| Lu Wushuang   |
| Ma Chao       |
| Ren Yingying  |
| Shi Potian    |
| Shi Qing      |
| Shi Zhongyu   |
| Sun Dasheng   |
| Tian Boguang  |
| Wen Qingqing  |
| Xi Ren        |
| Xiao Qiao     |
| Xie Yanke     |
| Xu Xian       |
| Xu Zhu        |
| Xue Baochai   |
| Yinjiao King  |
| Yu Yutong     |
| Yuan Chengzhi |
| Yue Lingshan  |
+---------------+
27 rows in set (0.00 sec)

算数和比较操作符

mysql> SELECT Name,Age FROM students WHERE Age > 50;
+--------------+-----+
| Name         | Age |
+--------------+-----+
| Xie Yanke    |  53 |
| Yinjiao King |  98 |
| Sun Dasheng  | 100 |
| Jinjiao King | 100 |
+--------------+-----+
4 rows in set (0.00 sec)

mysql> SELECT Name,Age FROM students WHERE Age+30 > 50;
+---------------+-----+
| Name          | Age |
+---------------+-----+
| Shi Zhongyu   |  22 |
| Shi Potian    |  22 |
| Xie Yanke     |  53 |
| Ding Dian     |  32 |
| Yu Yutong     |  26 |
| Shi Qing      |  46 |
| Yuan Chengzhi |  23 |
| Tian Boguang  |  33 |
| Xu Zhu        |  21 |
| Lin Chong     |  25 |
| Hua Rong      |  23 |
| Huang Yueying |  22 |
| Ma Chao       |  23 |
| Xu Xian       |  27 |
| Sun Dasheng   | 100 |
| Jinjiao King  | 100 |
| Yinjiao King  |  98 |
+---------------+-----+
17 rows in set (0.00 sec)

mysql> SELECT Name,Age FROM students WHERE Age != 50;
+---------------+-----+
| Name          | Age |
+---------------+-----+
| Shi Zhongyu   |  22 |
| Shi Potian    |  22 |
| Xie Yanke     |  53 |
| Ding Dian     |  32 |
| Yu Yutong     |  26 |
| Shi Qing      |  46 |
| Xi Ren        |  19 |
| Lin Daiyu     |  17 |
| Ren Yingying  |  20 |
| Yue Lingshan  |  19 |
| Yuan Chengzhi |  23 |
| Wen Qingqing  |  19 |
| Tian Boguang  |  33 |
| Lu Wushuang   |  17 |
| Duan Yu       |  19 |
| Xu Zhu        |  21 |
| Lin Chong     |  25 |
| Hua Rong      |  23 |
| Xue Baochai   |  18 |
| Diao Chan     |  19 |
| Huang Yueying |  22 |
| Xiao Qiao     |  20 |
| Ma Chao       |  23 |
| Xu Xian       |  27 |
| Sun Dasheng   | 100 |
| Jinjiao King  | 100 |
| Yinjiao King  |  98 |
+---------------+-----+
27 rows in set (0.00 sec)

mysql> SELECT Name,Age FROM students WHERE Age IN (18,100);
+--------------+-----+
| Name         | Age |
+--------------+-----+
| Xue Baochai  |  18 |
| Sun Dasheng  | 100 |
| Jinjiao King | 100 |
+--------------+-----+
3 rows in set (0.00 sec)

mysql> SELECT Name,ClassID FROM students WHERE ClassID = NULL;
Empty set (0.00 sec)

mysql> SELECT Name,ClassID FROM students WHERE ClassID IS  NULL;
+--------------+---------+
| Name         | ClassID |
+--------------+---------+
| Xu Xian      |    NULL |
| Sun Dasheng  |    NULL |
| Jinjiao King |    NULL |
| Yinjiao King |    NULL |
+--------------+---------+
4 rows in set (0.00 sec)

根据性别进行分组

mysql> SELECT * FROM students GROUP BY Age;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |
|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|    27 | Yinjiao King  |  98 | M      |    NULL |      NULL |
|    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |
+-------+---------------+-----+--------+---------+-----------+

根据性别进行分组并且年龄做平均运算,根据性别显示

GROUP BY:根据指定的字段把查询的结果进行“分组”以用于“聚合”运算;
avg(), max(), min(), sum(), count()
HAVING:对分组聚合后的结果进行条件过滤;

mysql> SELECT avg(Age),Gender FROM students GROUP BY Gender;
+----------+--------+
| avg(Age) | Gender |
+----------+--------+
|  19.0000 | F      |
|  40.7647 | M      |
+----------+--------+
2 rows in set (0.00 sec)

统计每个班级的人数(对班级进行分组)

mysql> SELECT count(StuID) AS NOS,ClassID FROM students GROUP BY ClassID;
+-----+---------+
| NOS | ClassID |
+-----+---------+
|   4 |    NULL |
|   4 |       1 |
|   3 |       2 |
|   4 |       3 |
|   4 |       4 |
|   1 |       5 |
|   4 |       6 |
|   3 |       7 |
+-----+---------+
8 rows in set (0.00 sec)

统计每个班级的人数并且大于2的

mysql> SELECT count(StuID) AS NOS,ClassID FROM students GROUP BY ClassID HAVING NOS>2;
+-----+---------+
| NOS | ClassID |
+-----+---------+
|   4 |    NULL |
|   4 |       1 |
|   3 |       2 |
|   4 |       3 |
|   4 |       4 |
|   4 |       6 |
|   3 |       7 |
+-----+---------+
7 rows in set (0.01 sec)

根据年龄排序,降序

ORDER BY:根据指定的字段把查询的结果进行排序;默认升序;
升序:ASC
降序:DESC

mysql> SELECT Name,Age FROM students ORDER BY Age DESC;
+---------------+-----+
| Name          | Age |
+---------------+-----+
| Jinjiao King  | 100 |
| Sun Dasheng   | 100 |
| Yinjiao King  |  98 |
| Xie Yanke     |  53 |
| Shi Qing      |  46 |
| Tian Boguang  |  33 |
| Ding Dian     |  32 |
| Xu Xian       |  27 |
| Yu Yutong     |  26 |
| Lin Chong     |  25 |
| Ma Chao       |  23 |
| Yuan Chengzhi |  23 |
| Hua Rong      |  23 |
| Huang Yueying |  22 |
| Shi Potian    |  22 |
| Shi Zhongyu   |  22 |
| Xu Zhu        |  21 |
| Xiao Qiao     |  20 |
| Ren Yingying  |  20 |
| Duan Yu       |  19 |
| Wen Qingqing  |  19 |
| Diao Chan     |  19 |
| Yue Lingshan  |  19 |
| Xi Ren        |  19 |
| Xue Baochai   |  18 |
| Lu Wushuang   |  17 |
| Lin Daiyu     |  17 |
+---------------+-----+
27 rows in set (0.00 sec)

查询之后显示10行

LIMIT:对输出结果进行数量限制
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
LIMIT 10:显示10行
LIMIT 10,10 :显示11-20行,偏移10行,显示后10行

mysql> SELECT Name,Age FROM students ORDER BY Age DESC LIMIT 10;
+--------------+-----+
| Name         | Age |
+--------------+-----+
| Sun Dasheng  | 100 |
| Jinjiao King | 100 |
| Yinjiao King |  98 |
| Xie Yanke    |  53 |
| Shi Qing     |  46 |
| Tian Boguang |  33 |
| Ding Dian    |  32 |
| Xu Xian      |  27 |
| Yu Yutong    |  26 |
| Lin Chong    |  25 |
+--------------+-----+
10 rows in set (0.00 sec)

mysql> SELECT Name,Age FROM students ORDER BY Age DESC LIMIT 10,10;
+---------------+-----+
| Name          | Age |
+---------------+-----+
| Ma Chao       |  23 |
| Yuan Chengzhi |  23 |
| Hua Rong      |  23 |
| Huang Yueying |  22 |
| Shi Potian    |  22 |
| Shi Zhongyu   |  22 |
| Xu Zhu        |  21 |
| Xiao Qiao     |  20 |
| Ren Yingying  |  20 |
| Duan Yu       |  19 |
+---------------+-----+
10 rows in set (0.00 sec)

二、多表查询

交叉连接,笛卡尔乘积

连接查询的结果集中包含一个表中的每一条记录与另一个表中的每一条记录相互匹配的组合,像这样的结果集就可以称之为笛卡尔积。
不推荐此方法查询。

mysql> mysql> SELECT students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
|    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |
|    15 | Duan Yu       |  19 | M      |       4 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |
|    20 | Diao Chan     |  19 | F      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
|    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |
|    26 | Jinjiao King  | 100 | M      |    NULL |      NULL |
|    27 | Yinjiao King  |  98 | M      |    NULL |      NULL |
+-------+---------------+-----+--------+---------+-----------+
27 rows in set (0.00 sec)
mysql> SELECT * FROM teachers;;
+-----+---------------+-----+--------+
| TID | Name          | Age | Gender |
+-----+---------------+-----+--------+
|   1 | Song Jiang    |  45 | M      |
|   2 | Zhang Sanfeng |  94 | M      |
|   3 | Miejue Shitai |  77 | F      |
|   4 | Lin Chaoying  |  93 | F      |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM students,teachers;;
+-------+---------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
| StuID | Name          | Age | Gender | ClassID | TeacherID | TID | Name          | Age | Gender |
+-------+---------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |   1 | Song Jiang    |  45 | M      |
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |   2 | Zhang Sanfeng |  94 | M      |
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |   3 | Miejue Shitai |  77 | F      |
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |   4 | Lin Chaoying  |  93 | F      |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |   1 | Song Jiang    |  45 | M      |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |   2 | Zhang Sanfeng |  94 | M      |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |   3 | Miejue Shitai |  77 | F      |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |   4 | Lin Chaoying  |  93 | F      |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |   1 | Song Jiang    |  45 | M      |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |   2 | Zhang Sanfeng |  94 | M      |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |   3 | Miejue Shitai |  77 | F      |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |   4 | Lin Chaoying  |  93 | F      |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |   1 | Song Jiang    |  45 | M      |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |   2 | Zhang Sanfeng |  94 | M      |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |   3 | Miejue Shitai |  77 | F      |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |   4 | Lin Chaoying  |  93 | F      |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |   1 | Song Jiang    |  45 | M      |
...
...
|    27 | Yinjiao King  |  98 | M      |    NULL |      NULL |   3 | Miejue Shitai |  77 | F      |
|    27 | Yinjiao King  |  98 | M      |    NULL |      NULL |   4 | Lin Chaoying  |  93 | F      |
+-------+---------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
108 rows in set (0.00 sec)

内连接

等值连接:
让表之间的字段以等值的方式建立连接,
两个表中具有相同意义的字段或具有关联的字段建立连接。

  • 等值连接
mysql> SELECT * FROM students,teachers WHERE students.TeacherID=teachers.TID;
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
| StuID | Name        | Age | Gender | ClassID | TeacherID | TID | Name          | Age | Gender |
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
|     1 | Shi Zhongyu |  22 | M      |       2 |         3 |   3 | Miejue Shitai |  77 | F      |
|     4 | Ding Dian   |  32 | M      |       4 |         4 |   4 | Lin Chaoying  |  93 | F      |
|     5 | Yu Yutong   |  26 | M      |       3 |         1 |   1 | Song Jiang    |  45 | M      |
+-------+-------------+-----+--------+---------+-----------+-----+---------------+-----+--------+
3 rows in set (0.00 sec)

查询每个同学所属的门派

mysql> SELECT * FROM students;
+-------+---------------+-----+--------+---------+-----------+
| StuID | Name          | Age | Gender | ClassID | TeacherID |
+-------+---------------+-----+--------+---------+-----------+
|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |
|     2 | Shi Potian    |  22 | M      |       1 |         7 |
|     3 | Xie Yanke     |  53 | M      |       2 |        16 |
|     4 | Ding Dian     |  32 | M      |       4 |         4 |
|     5 | Yu Yutong     |  26 | M      |       3 |         1 |
|     6 | Shi Qing      |  46 | M      |       5 |      NULL |
|     7 | Xi Ren        |  19 | F      |       3 |      NULL |
|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |
|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |
|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |
|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |
|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |
|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |
|    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |
|    15 | Duan Yu       |  19 | M      |       4 |      NULL |
|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |
|    17 | Lin Chong     |  25 | M      |       4 |      NULL |
|    18 | Hua Rong      |  23 | M      |       7 |      NULL |
|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |
|    20 | Diao Chan     |  19 | F      |       7 |      NULL |
|    21 | Huang Yueying |  22 | F      |       6 |      NULL |
|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |
|    23 | Ma Chao       |  23 | M      |       4 |      NULL |
|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |
|    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |
|    26 | Jinjiao King  | 100 | M      |    NULL |      NULL |
|    27 | Yinjiao King  |  98 | M      |    NULL |      NULL |
+-------+---------------+-----+--------+---------+-----------+
27 rows in set (0.00 sec)

mysql> SELECT * FROM classes;
+---------+----------------+----------+
| ClassID | Class          | NumOfStu |
+---------+----------------+----------+
|       1 | Shaolin Pai    |       10 |
|       2 | Emei Pai       |        7 |
|       3 | QingCheng Pai  |       11 |
|       4 | Wudang Pai     |       12 |
|       5 | Riyue Shenjiao |       31 |
|       6 | Lianshan Pai   |       27 |
|       7 | Ming Jiao      |       27 |
|       8 | Xiaoyao Pai    |       15 |
+---------+----------------+----------+
8 rows in set (0.00 sec)

mysql> SELECT students.Name,classes.Class FROM students,classes WHERE students.ClassID=classes.ClassID;
+---------------+----------------+
| Name          | Class          |
+---------------+----------------+
| Shi Zhongyu   | Emei Pai       |
| Shi Potian    | Shaolin Pai    |
| Xie Yanke     | Emei Pai       |
| Ding Dian     | Wudang Pai     |
| Yu Yutong     | QingCheng Pai  |
| Shi Qing      | Riyue Shenjiao |
| Xi Ren        | QingCheng Pai  |
| Lin Daiyu     | Ming Jiao      |
| Ren Yingying  | Lianshan Pai   |
| Yue Lingshan  | QingCheng Pai  |
| Yuan Chengzhi | Lianshan Pai   |
| Wen Qingqing  | Shaolin Pai    |
| Tian Boguang  | Emei Pai       |
| Lu Wushuang   | QingCheng Pai  |
| Duan Yu       | Wudang Pai     |
| Xu Zhu        | Shaolin Pai    |
| Lin Chong     | Wudang Pai     |
| Hua Rong      | Ming Jiao      |
| Xue Baochai   | Lianshan Pai   |
| Diao Chan     | Ming Jiao      |
| Huang Yueying | Lianshan Pai   |
| Xiao Qiao     | Shaolin Pai    |
| Ma Chao       | Wudang Pai     |
+---------------+----------------+
23 rows in set (0.00 sec)
  • 自连接
mysql> SELECT s.Name,t.Name FROM students AS s,students AS t WHERE s.TeacherID=t.StuID;
+-------------+-------------+
| Name        | Name        |
+-------------+-------------+
| Shi Zhongyu | Xie Yanke   |
| Shi Potian  | Xi Ren      |
| Xie Yanke   | Xu Zhu      |
| Ding Dian   | Ding Dian   |
| Yu Yutong   | Shi Zhongyu |
+-------------+-------------+
5 rows in set (0.00 sec)

外连接

左外连接:以左边表为基准,建立连接,左右都有值的显示值,左边有的右边没有,显示为空。
右外连接:以右边表为基准,建立连接,左右都有值的显示值,右边有的左边没有,显示为空。

  • 左外连接
mysql> mysql> SELECT s.Name,c.Class FROM students AS s LEFT JOIN classes AS c ON s.ClassID=c.ClassID;
+---------------+----------------+
| Name          | Class          |
+---------------+----------------+
| Shi Zhongyu   | Emei Pai       |
| Shi Potian    | Shaolin Pai    |
| Xie Yanke     | Emei Pai       |
| Ding Dian     | Wudang Pai     |
| Yu Yutong     | QingCheng Pai  |
| Shi Qing      | Riyue Shenjiao |
| Xi Ren        | QingCheng Pai  |
| Lin Daiyu     | Ming Jiao      |
| Ren Yingying  | Lianshan Pai   |
| Yue Lingshan  | QingCheng Pai  |
| Yuan Chengzhi | Lianshan Pai   |
| Wen Qingqing  | Shaolin Pai    |
| Tian Boguang  | Emei Pai       |
| Lu Wushuang   | QingCheng Pai  |
| Duan Yu       | Wudang Pai     |
| Xu Zhu        | Shaolin Pai    |
| Lin Chong     | Wudang Pai     |
| Hua Rong      | Ming Jiao      |
| Xue Baochai   | Lianshan Pai   |
| Diao Chan     | Ming Jiao      |
| Huang Yueying | Lianshan Pai   |
| Xiao Qiao     | Shaolin Pai    |
| Ma Chao       | Wudang Pai     |
| Xu Xian       | NULL           |
| Sun Dasheng   | NULL           |
| Jinjiao King  | NULL           |
| Yinjiao King  | NULL           |
+---------------+----------------+
27 rows in set (0.00 sec)
  • 右外连接
 mysql> SELECT s.Name,c.Class FROM students AS s RIGHT JOIN classes AS c ON s.ClassID=c.ClassID;
+---------------+----------------+
| Name          | Class          |
+---------------+----------------+
| Shi Zhongyu   | Emei Pai       |
| Shi Potian    | Shaolin Pai    |
| Xie Yanke     | Emei Pai       |
| Ding Dian     | Wudang Pai     |
| Yu Yutong     | QingCheng Pai  |
| Shi Qing      | Riyue Shenjiao |
| Xi Ren        | QingCheng Pai  |
| Lin Daiyu     | Ming Jiao      |
| Ren Yingying  | Lianshan Pai   |
| Yue Lingshan  | QingCheng Pai  |
| Yuan Chengzhi | Lianshan Pai   |
| Wen Qingqing  | Shaolin Pai    |
| Tian Boguang  | Emei Pai       |
| Lu Wushuang   | QingCheng Pai  |
| Duan Yu       | Wudang Pai     |
| Xu Zhu        | Shaolin Pai    |
| Lin Chong     | Wudang Pai     |
| Hua Rong      | Ming Jiao      |
| Xue Baochai   | Lianshan Pai   |
| Diao Chan     | Ming Jiao      |
| Huang Yueying | Lianshan Pai   |
| Xiao Qiao     | Shaolin Pai    |
| Ma Chao       | Wudang Pai     |
| NULL          | Xiaoyao Pai    |
+---------------+----------------+
24 rows in set (0.00 sec)

子查询

  • 查询年龄大于平均年龄的同学
mysql> SELECT Name,Age FROM students WHERE Age> (SELECT avg(Age) FROM students);
+--------------+-----+
| Name         | Age |
+--------------+-----+
| Tian Boguang |  33 |
| Shi Qing     |  46 |
| Xie Yanke    |  53 |
| Yinjiao King |  98 |
| Sun Dasheng  | 100 |
| Jinjiao King | 100 |
+--------------+-----+
6 rows in set (0.00 sec)
  • 用于IN中的子查询
mysql> SELECT Name,Age FROM students WHERE Age IN (SELECT Age FROM teachers);
Empty set (0.01 sec)

联合查询:将多个查询语句的执行结果相合并

mysql> SELECT Name,Age FROM students UNION SELECT Name,Age FROM teachers;
+---------------+-----+
| Name          | Age |
+---------------+-----+
| Shi Zhongyu   |  22 |
| Shi Potian    |  22 |
| Xie Yanke     |  53 |
| Ding Dian     |  32 |
| Yu Yutong     |  26 |
| Shi Qing      |  46 |
| Xi Ren        |  19 |
| Lin Daiyu     |  17 |
| Ren Yingying  |  20 |
| Yue Lingshan  |  19 |
| Yuan Chengzhi |  23 |
| Wen Qingqing  |  19 |
| Tian Boguang  |  33 |
| Lu Wushuang   |  17 |
| Duan Yu       |  19 |
| Xu Zhu        |  21 |
| Lin Chong     |  25 |
| Hua Rong      |  23 |
| Xue Baochai   |  18 |
| Diao Chan     |  19 |
| Huang Yueying |  22 |
| Xiao Qiao     |  20 |
| Ma Chao       |  23 |
| Xu Xian       |  27 |
| Sun Dasheng   | 100 |
| Jinjiao King  | 100 |
| Yinjiao King  |  98 |
| Song Jiang    |  45 |
| Zhang Sanfeng |  94 |
| Miejue Shitai |  77 |
| Lin Chaoying  |  93 |
+---------------+-----+
31 rows in set (0.00 sec)

标签:语句,Name,mysql,Age,Mysql,查询,Shi,Pai,NULL
From: https://www.cnblogs.com/OpenSourceSite/p/18061322

相关文章

  • 开启 mysql 的 general_log
    在做等保评测时,会要求mysql开启general_log日志,该日志会记录所有的数据库动作,增长幅度非常大,因此适合于在出现问题时临时开启一段时间,待问题排查解决后再进行关闭,否则日志文件的增长速度会超出你的想象。1、首先来看一下关于general_log的几个参数: mysql>showvariable......
  • MySql中SUM函数计算错误问题
    前言今天一个很久前做的项目突然找到我,说是之前做的项目中,页面上数据汇总和列表中的数据的总数存在对不上的问题。说是列表是对的,但是根据列表统计出来的数据要比正常小很多。排查这个项目已经好几年了,之前用了很久都是正常的,不可能会突然出问题了;我觉得这个统计肯定是没问题了......
  • MYSQL学习笔记22: 多表查询
    多表查询单表查询查询emp表select*fromemp;查询dept表select*fromdept;笛卡尔积(全组合)#emp表有4条记录,dept表有6条记录#笛卡尔积有4*6=24条记录select*fromemp,dept;消除无效的笛卡尔积(emp和dept通过dept_id连接)select*fromemp,deptw......
  • 分支和循环语句
    0表示真,非零表示假语句——由“;”隔开的就是一条语句分支语句(选择结构)ifswitch 循环语句(循环结构)whilefordowhilegoto语句(1)if语句1,if语句语法结构if(表达式)                          if(表达式)   ......
  • MySQL 入门指南
    目录1.简介和基础概念1.1.MySQL是什么?1.2.数据库管理系统(DBMS)的基本概念1.3.SQL(StructuredQueryLanguage)的作用和重要性2.安装和配置MySQL2.1.在Linux下使用Docker进行MySQL的安装和配置2.2.Windows、MacOS下的安装步骤(待补充)3.SQL基础3.1.SQL的基本语法......
  • SQL mother查询语句
    题目教程网址:SQL之母基础查询select*fromstudent;selectxxx,xxxxasname,ddd,xxx*2asdouble_xxxfroma_tablewherexx>10ordd!=1andkk=9;别名,常量和运算,where,运算符(=,!=,>,<),逻辑运算(and,or,not)。模糊查询selectsomethingfroma_t......
  • 使用Tokeniser估算GPT和LLM服务的查询成本
    将LLM集成到项目所花费的成本主要是我们通过API获取LLM返回结果的成本,而这些成本通常是根据处理的令牌数量计算的。我们如何预估我们的令牌数量呢?Tokeniser包可以有效地计算文本输入中的令牌来估算这些成本。本文将介绍如何使用Tokeniser有效地预测和管理费用。大语言模型(如GPT)......
  • MYSQl学习笔记19: 外键约束
    外键约束用来让两张表的数据之间建立连接,从而保证数据的一致性和完整性具有外键的表(emp)称为子表外键关联的表(dept)称为父表外键约束创建表时添加createtable表名(字段名数据类型,[constrain][外键名称]foreignkey(外键字段名)references主表(主表......
  • MYSQL学习笔记20: 外键约束(删除/更新行为)
    外键约束删除/更新行为setdefault在mysql的默认引擎innodb中不支持CASCADEaltertable表名addconstraint外键名称foreignkey(外键字段)references主表名(主表字段名)onupdatecascadeondeletecascade;建立外键约束#如果父表和子表建立外键的字段有不同的......
  • MYSQL学习笔记17: 流程控制函数(IF, CASE)
    流程控制函数(IF,CASE)ifselectif(true,'ok','error');selectif(false,'ok','error');/*相当于iftrue:ok;else:error;*/ifnullselectifnull('ok','default');selectifnull(......