8:单表查询-MySQL

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 8:单表查询-MySQL

8.1 开端

细说查询语句

8.2 select

可以查询不存在的表,也可以做计算,别名操作使用as 或者省略

mysql> select 2*7 as res;
+-----+
| res |
+-----+
|  14 |
+-----+
1 row in set (0.00 sec)

8.3 from

来自哪张表,返回笛卡尔积

mysql> select * from stu;
+-------+-------+
| stuId | name  |
+-------+-------+
|     4 | frank |
+-------+-------+
1 row in set (0.00 sec)
mysql> select * from eatery;
+----+----------+-------+
| id | money    | stuId |
+----+----------+-------+
|  1 |  20.5000 |  NULL |
|  2 |  78.6000 |     4 |
|  3 |  99.9000 |  NULL |
|  4 | 748.4000 |     4 |
|  5 | 748.4000 |  NULL |
+----+----------+-------+
5 rows in set (0.00 sec)
mysql> select * from stu,eatery;
+-------+-------+----+----------+-------+
| stuId | name  | id | money    | stuId |
+-------+-------+----+----------+-------+
|     4 | frank |  1 |  20.5000 |  NULL |
|     4 | frank |  2 |  78.6000 |     4 |
|     4 | frank |  3 |  99.9000 |  NULL |
|     4 | frank |  4 | 748.4000 |     4 |
|     4 | frank |  5 | 748.4000 |  NULL |
+-------+-------+----+----------+-------+
5 rows in set (0.00 sec)

8.4 dual

dual表丝滑默认的伪表,你可以在没有表的情况下指定一个虚拟的表名

mysql> select 2*7 as res from dual; /*计算器*/
+-----+
| res |
+-----+
|  14 |
+-----+
1 row in set (0.00 sec)

8.5 where

1.用来筛选

mysql> select * from teacher;
+----+------+--------+----------+
| id | name | phone  | address  |
+----+------+--------+----------+
|  1 | Tom  | NULL   | 暂时未知 |
|  2 | Tom  | NULL   | 暂时未知 |
|  3 | Tom  | 123456 | ShangHai |
|  4 | Tom  | NULL   | NULL     |
+----+------+--------+----------+
4 rows in set (0.00 sec)
mysql> select * from teacher where id = 1;
+----+------+-------+----------+
| id | name | phone | address  |
+----+------+-------+----------+
|  1 | Tom  | NULL  | 暂时未知 |
+----+------+-------+----------+
1 row in set (0.00 sec)

2.设置< > = != and or 等条件

mysql> select * from teacher where id = 1 or phone = '123456';
+----+------+--------+----------+
| id | name | phone  | address  |
+----+------+--------+----------+
|  1 | Tom  | NULL   | 暂时未知 |
|  3 | Tom  | 123456 | ShangHai |
+----+------+--------+----------+
2 rows in set (0.00 sec)

8.6 in

in代替等号的一种,或者用not in代替不等号

mysql> select * from teacher where address in ('ShangHai');
+----+------+--------+----------+
| id | name | phone  | address  |
+----+------+--------+----------+
|  3 | Tom  | 123456 | ShangHai |
+----+------+--------+----------+
1 row in set (0.00 sec)
mysql> select * from teacher where address not in ('ShangHai');
+----+------+-------+----------+
| id | name | phone | address  |
+----+------+-------+----------+
|  1 | Tom  | NULL  | 暂时未知 |
|  2 | Tom  | NULL  | 暂时未知 |
+----+------+-------+----------+
2 rows in set (0.00 sec)

8.7 between…and

> and < 不同,between and表示在什么之间,会取等于两个数字之间的值

mysql>  select * from teacher where id >1 and id < 4;
+----+------+--------+----------+
| id | name | phone  | address  |
+----+------+--------+----------+
|  2 | Tom  | NULL   | 暂时未知 |
|  3 | Tom  | 123456 | ShangHai |
+----+------+--------+----------+
2 rows in set (0.00 sec)
mysql>  select * from teacher where id between 1 and 4;
+----+------+--------+----------+
| id | name | phone  | address  |
+----+------+--------+----------+
|  1 | Tom  | NULL   | 暂时未知 |
|  2 | Tom  | NULL   | 暂时未知 |
|  3 | Tom  | 123456 | ShangHai |
|  4 | Tom  | NULL   | NULL     |
+----+------+--------+----------+
4 rows in set (0.00 sec)
mysql>  select * from teacher where id not between 1 and 4;
Empty set (0.00 sec)

8.8 is null

查询数据是否为空

mysql>  select * from teacher;
+----+------+--------+----------+
| id | name | phone  | address  |
+----+------+--------+----------+
|  1 | Tom  | NULL   | 暂时未知 |
|  2 | Tom  | NULL   | 暂时未知 |
|  3 | Tom  | 123456 | ShangHai |
|  4 | Tom  | NULL   | NULL     |
+----+------+--------+----------+
4 rows in set (0.00 sec)
mysql> select * from teacher where address is null;
+----+------+-------+---------+
| id | name | phone | address |
+----+------+-------+---------+
|  4 | Tom  | NULL  | NULL    |
+----+------+-------+---------+
1 row in set (0.00 sec)
mysql> select * from teacher where address is not null;
+----+------+--------+----------+
| id | name | phone  | address  |
+----+------+--------+----------+
|  1 | Tom  | NULL   | 暂时未知 |
|  2 | Tom  | NULL   | 暂时未知 |
|  3 | Tom  | 123456 | ShangHai |
+----+------+--------+----------+
3 rows in set (0.00 sec)

8.9 聚合函数

sumavgcount等函数的使用

mysql> select * from score;
+------+---------+---------+------+
| id   | chinese | english | math |
+------+---------+---------+------+
|    1 |      99 |      99 |   99 |
|    2 |      77 |      94 |   96 |
+------+---------+---------+------+
2 rows in set (0.00 sec)
mysql> select sum(chinese) from score;
+--------------+
| sum(chinese) |
+--------------+
|          176 |
+--------------+
1 row in set (0.00 sec)
mysql> select avg(chinese) from score;
+--------------+
| avg(chinese) |
+--------------+
|      88.0000 |
+--------------+
1 row in set (0.00 sec)
mysql> select count(chinese) from score;
+----------------+
| count(chinese) |
+----------------+
|              2 |
+----------------+
1 row in set (0.00 sec)

统计所有数据的总数

count(1)计算一共有多少符合条件的行,1并不是表示第一个字段,而是表示一个固定值

count(*)(是针对全表)将返回表格中所有存在的行的总数包括值为null的行

count(列名)(是针对某一列)将返回表格中某一列除去null以外的所有行的总数

mysql> select count(*) from score;
+----------+
| count(*) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)
mysql> select count(1) from score;
+----------+
| count(1) |
+----------+
|        2 |
+----------+
1 row in set (0.00 sec)

8.10 第三方客户端的使用

学习使用Navicat Premium 15

8.11 like模糊查询

% 表示任意0个或多个字符

_ 表示任意单个字符

mysql> select * from teacher where name like 'T%';
+----+------+--------+----------+
| id | name | phone  | address  |
+----+------+--------+----------+
|  1 | Tom  | NULL   | 暂时未知 |
|  2 | Tom  | NULL   | 暂时未知 |
|  3 | Tom  | 123456 | ShangHai |
|  4 | Tom  | NULL   | NULL     |
+----+------+--------+----------+
4 rows in set (0.00 sec)
mysql> select * from teacher where name like 'T_';
Empty set (0.00 sec)
mysql> select * from teacher where name like 'To_';
+----+------+--------+----------+
| id | name | phone  | address  |
+----+------+--------+----------+
|  1 | Tom  | NULL   | 暂时未知 |
|  2 | Tom  | NULL   | 暂时未知 |
|  3 | Tom  | 123456 | ShangHai |
|  4 | Tom  | NULL   | NULL     |
+----+------+--------+----------+
4 rows in set (0.00 sec)

8.12 order by 排序查询

desc表示降序,asc表示升序

mysql> select * from score;
+------+---------+---------+------+
| id   | chinese | english | math |
+------+---------+---------+------+
|    1 |      99 |      99 |   99 |
|    2 |      77 |      94 |   96 |
+------+---------+---------+------+
2 rows in set (0.00 sec)
mysql> select * from score order by chinese desc;
+------+---------+---------+------+
| id   | chinese | english | math |
+------+---------+---------+------+
|    1 |      99 |      99 |   99 |
|    2 |      77 |      94 |   96 |
+------+---------+---------+------+
2 rows in set (0.00 sec)
mysql> select * from score order by chinese asc;
+------+---------+---------+------+
| id   | chinese | english | math |
+------+---------+---------+------+
|    2 |      77 |      94 |   96 |
|    1 |      99 |      99 |   99 |
+------+---------+---------+------+
2 rows in set (0.00 sec)

8.13 group by 分组查询

mysql> select 聚合函数 as 'xx' ,分组字段 as 'xx' ' from info group by xx;
mysql> select avg(age) as '年龄' ,gender as '性别' ' from info group by gender;

查询的字段必须是分组字段和聚合函数

8.14 group_concat

concat()函数将多个字符串连接成一个字符串

group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'])
mysql> select * from eatery;
+----+----------+-------+
| id | money    | stuId |
+----+----------+-------+
|  1 |  20.5000 |  NULL |
|  2 |  78.6000 |     4 |
|  3 |  99.9000 |  NULL |
|  4 | 748.4000 |     4 |
|  5 | 748.4000 |  NULL |
+----+----------+-------+
5 rows in set (0.00 sec)
mysql> select stuId ,group_concat(money) from eatery group by stuId;
+-------+--------------------------+
| stuId | group_concat(money)      |
+-------+--------------------------+
|  NULL | 20.5000,99.9000,748.4000 |
|     4 | 78.6000,748.4000         |
+-------+--------------------------+
2 rows in set (0.00 sec)

通过使用distinct可以排除重复值

mysql> select stuId ,group_concat(distinct money) from eatery group by stuId;
+-------+------------------------------+
| stuId | group_concat(distinct money) |
+-------+------------------------------+
|  NULL | 20.5000,99.9000,748.4000     |
|     4 | 78.6000,748.4000             |
+-------+------------------------------+
2 rows in set (0.00 sec)

使用order by子句对结果中的值进行排序

mysql> select stuId ,group_concat(distinct money order by money desc) from eatery group by stuId;
+-------+--------------------------------------------------+
| stuId | group_concat(distinct money order by money desc) |
+-------+--------------------------------------------------+
|  NULL | 748.4000,99.9000,20.5000                         |
|     4 | 748.4000,78.6000                                 |
+-------+--------------------------------------------------+
2 rows in set (0.00 sec)

当然可以非常长,separator是一个字符串值,缺省为一个逗号

mysql> select stuId ,group_concat(distinct money order by money desc separator '||') from eatery group by stuId;
+-------+-----------------------------------------------------------------+
| stuId | group_concat(distinct money order by money desc separator '||') |
+-------+-----------------------------------------------------------------+
|  NULL | 748.4000||99.9000||20.5000                                      |
|     4 | 748.4000||78.6000                                               |
+-------+-----------------------------------------------------------------+
2 rows in set (0.00 sec)

8.15 having

在 SQL 中增加having子句原因是where关键字无法与合计函数一起使用

having子句可以让我们筛选分组后的各组数据。

mysql> select avg(money) as '$' ,stuId as 'stuId' from eatery group by stuId having $>400;
+--------------+-------+
| $            | stuId |
+--------------+-------+
| 413.50000000 |     4 |
+--------------+-------+
1 row in set (0.00 sec)

8.16 limit

limit子句被用于强制select语句返回指定的记录数,初始记录行的偏移量是0而不是1

mysql> select * from eatery limit 0,2;
+----+---------+-------+
| id | money   | stuId |
+----+---------+-------+
|  1 | 20.5000 |  NULL |
|  2 | 78.6000 |     4 |
+----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from eatery limit 3;
+----+---------+-------+
| id | money   | stuId |
+----+---------+-------+
|  1 | 20.5000 |  NULL |
|  2 | 78.6000 |     4 |
|  3 | 99.9000 |  NULL |
+----+---------+-------+
3 rows in set (0.00 sec)

8.17 distinct all

去重

mysql> select * from eatery;
+----+----------+-------+
| id | money    | stuId |
+----+----------+-------+
|  1 |  20.5000 |  NULL |
|  2 |  78.6000 |     4 |
|  3 |  99.9000 |  NULL |
|  4 | 748.4000 |     4 |
|  5 | 748.4000 |  NULL |
+----+----------+-------+
5 rows in set (0.00 sec)
mysql> select distinct money from eatery;
+----------+
| money    |
+----------+
|  20.5000 |
|  78.6000 |
|  99.9000 |
| 748.4000 |
+----------+
4 rows in set (0.00 sec)


相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
算法 关系型数据库 MySQL
【MySQL系列】Select语句单表查询详解(二)ORDERBY排序
文章目录 一、排序ORDER BY 1.ORDER BY的格式: 2.ORDER BY的作用: 3.ORDER BY的使用: 1️⃣ 按照字段名排序 2️⃣按字段照序号进行排序 3️⃣降序DESC 4️⃣根据多列排序 5️⃣还可以结合搭配函数或者一些查询条件使用。
|
6月前
|
SQL 关系型数据库 MySQL
MySql基础三之【单表查询进阶操作】
MySql基础三之【单表查询进阶操作】
52 0
|
7月前
|
关系型数据库 MySQL 大数据
Mysql单表查询例题详解
Mysql单表查询例题详解
|
7月前
|
关系型数据库 MySQL
Mysql单表查询的基本操作
Mysql单表查询的基本操作
|
SQL 算法 关系型数据库
【MySQL系列】Select语句单表查询详解入门(SELECT,AS,模糊查询,运算符,逻辑运算符)下
🌿五、BETWEEN...AND... BETWEEN作用: BETWEEN使用: 📌六 、模糊查询(LIKE) 1.通配符(`_ ` 和` %`) 2. 模糊查询的实现方式 3.使用: 2.例如 🌸七、逻辑运算符(and,or,not ) 1.and,or ,not作用: 2.and使用: 3.补充
|
安全 算法 关系型数据库
【MySQL系列】Select语句单表查询详解入门(SELECT,AS,模糊查询,运算符,逻辑运算符)上
文章目录 🌈一、Select语句入门 1.Select语句基本格式: 2.格式 🔎说明: 📃二、AS(起别名) 1.AS格式: 2.AS的使用: 🌟三、运算符>,<,=,<=…… 1.格式: 2.使用: 3.安全等于运算符: ‘<=>’ 💭四、 IN 和 NOT IN 1.in作用: 2.in的特点; 2.in的使用 3.补充
|
SQL 关系型数据库 MySQL
MySQL复合索引和单列索引的单表查询分析
MySQL复合索引和单列索引的单表查询分析
|
存储 关系型数据库 MySQL
Mysql从入门到入神之(五)表空间和单表查询
前言 文本已收录至我的GitHub仓库,欢迎Star:github.com/bin39232820… 种一棵树最好的时间是十年前,其次是现在
169 0
|
SQL 关系型数据库 MySQL
mysql加强(2)~单表查询、mysql查询常用的函数
mysql加强(2)~单表查询、mysql查询常用的函数
163 0
mysql加强(2)~单表查询、mysql查询常用的函数
|
SQL 存储 关系型数据库
关于mysql,需要掌握的基础(一):CRUD、存储引擎、单表查询相关、多表查询join、事务并发、权限管理等等
关于mysql,需要掌握的基础(一):CRUD、存储引擎、单表查询相关、多表查询join、事务并发、权限管理等等
143 0