MySQL数据库的查询:常用查询语句、MySQL函数、多表查询、视图表

一.构造数据

为了操作方便, 先构造以下数据:
1.学生表
表结构:

create table `student` (
    `id` int unsigned primary key auto_increment,
    `name` char(32) not null unique,
    `gender` enum('男', '⼥') not null,
    `city` char(32) not null,
    `description` text,
    `birthday` date not null default '1995-1-1',
    `money` float(7, 2) default 0,
    `only_child` boolean
) charset=utf8mb4;

表数据:

insert into `student` 
(`name`, `gender`, `city`, `description`, `birthday`, `money`, `only_child`)  values 
('郭德纲', '男', '北京', '班⻓', '1997/10/1', rand() * 100, True), 
('陈乔恩', '⼥', '上海', NULL, '1995/3/2', rand() * 100, True), 
('赵丽颖', '⼥', '北京', '班花, 不骄傲', '1995/4/4', rand() * 100, False), 
('王宝强', '男', '重庆', '超爱吃⽕锅', '1998/10/5', rand() * 100, False), 
('赵雅芝', '⼥', '重庆', '全宇宙三好学⽣', '1996/7/9', rand() * 100, True), 
('张学友', '男', '上海', '奥林匹克总冠军!', '1993/5/2', rand() * 100, False), 
('陈意涵', '⼥', '上海', NULL, '1994/8/30', rand() * 100, True), 
('赵本⼭', '男', '南京', '副班⻓', '1995/6/1', rand() * 100, True), 
('张柏芝', '⼥', '上海', NULL, '1997/2/28', rand() * 100, False), 
('吴亦凡', '男', '南京', '⼤碗宽⾯要不要?', '1995/6/1', rand() * 100, True), 
('⿅晗', '男', '北京', NULL, '1993/5/28', rand() * 100, True), 
('关晓彤', '⼥', '北京', NULL, '1995/7/12', rand() * 100, True), 
('周杰伦', '男', '台北', '⼩伙⼈才啊', '1998/3/28', rand() * 100, False), 
('⻢云', '男', '南京', '⼀个字:贼有钱', '1990/4/1', rand() * 100, False), 
('⻢化腾', '男', '上海', '⻢云死对头', '1990/11/28', rand() * 100, False); 

2.成绩表
表结构:

create table score ( 
        `id` int unsigned primary key auto_increment, 
        `math` float not null default 0, 
        `english` float not null default 0 
) charset=utf8mb4;

表数据:

insert into score (`math`, `english`) values 
(49, 71), (62, 66.7), (44, 86), (77.5, 74), (41, 75), 
(82, 59.5), (64.5, 85), (62, 98), (44, 36), (67, 56), 
(81, 90), (78, 70), (83, 66), (40, 90), (90, 90);  

二.常用的查询语句

1.SELECT:字段表达式

SELECT 既可以做查询,也可以做输出

select id, name from student; 
select rand(); -- 随机数
select unix_timestamp(); -- 显示Unix时间戳

运行结果:
在这里插入图片描述
在这里插入图片描述

2.FROM 子句

语法:select 字段 from 表名;
FROM 后面是数据源,数据源可以写多个,数据源⼀般是表明,也可以是其他查询的结果。

select student.name,score.english from student,score; 

在这里插入图片描述

3.WHERE 子句:按指定条件过滤

语法: select 字段 from 表名 where 条件;
WHERE 是做条件查询, 只返回结果为 True 的数据

select name from student where city = '北京';

在这里插入图片描述
空值判断:is null | is not null

select `name` from `student` where `description` is null;
select `name` from `student` where `description` is not null;

在这里插入图片描述
范围判断:between … and … | not between … and …

select id, math from score where math between 80 and 90; 

在这里插入图片描述

4.GROUP BY:分组查询

按照某⼀字段进行分组,会把该字段中值相同的归为⼀组,将查询的结果分类显示,方便统计。
如果有 WHERE 要放在 WHERE 的后面
语法: select 字段 from 表名 group by 分组字段;

select city, group_concat(name) from student group by city; 
select gender, group_concat(name) from student where city='北京' group by gender;

在这里插入图片描述
在这里插入图片描述

5.HAVING

HAVING 与 WHERE 在 SQL 中增加 HAVING 子句原因是:WHERE 关键字无法与聚合函数⼀起使用
语法:SELECT 字段 FROM 表名 HAVING 条件;
WHERE:后面不能加上聚合函数,只能写在数据源的后面
HAVING:条件字段必须要在结果集中出现, HAVING 可以写在 GROUP BY 的后面

select `id`,`name` from `student` having `id` >= 10;
select id,name,city from student where id>=3 having city='上海';
select city, group_concat(name),group_concat(birthday) from student group by city having min(birthday) > '1995-1-1'; 

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6.ORDER BY:按字段排序

ORDER BY 主要作用是排序
ORDER BY 写在 GROUPBY 后面,如果有 HAVING 也要写在 HAVING 的后面
语法: select 字段 from 表名 order by 排序字段 asc | desc;
   分为升序 asc 降序 desc, 默认 asc (可以不写)

select name,birthday from student order by birthday;
select name,birthday from student order by birthday desc;

在这里插入图片描述
在这里插入图片描述

7.LIMIT:限制取出数量

语法

select 字段 from 表名 limit m; --从第 1 行到第 m 行
select 字段 from 表名 limit m, n; -- 跳过前 m 行,往下取 n ⾏
select 字段 from 表名 limit m offset n; -- 跳过前 n ⾏, 取后⾯的 m ⾏

我这里是因为表中数据id不是升序,为了可以方便观看limit功能,使用order by将id升序
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

8.DISTINCT:去重

select distinct city from student;

在这里插入图片描述

9.dual表

dual 是⼀个虚拟表, 仅仅为了保证 select … from … 语句的完整性,无实际意义

select now() from dual;

在这里插入图片描述

三.函数

1.聚合函数

在这里插入图片描述
部分截图:
在这里插入图片描述
在这里插入图片描述

2.数值计算类函数

在这里插入图片描述
部分截图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.日期计算类函数

在这里插入图片描述
部分截图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.字符串相关函数

在这里插入图片描述
部分截图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5.其他函数

在这里插入图片描述
部分截图:
在这里插入图片描述

四.多表查询

1.UNION联合查询

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
union要求:

  1. 两边 select 语句的字段数必须⼀样
  2. 两边可以具有不同数据类型的字段
  3. 字段名默认按照左边的表来设置

union用法:

SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;

在这里插入图片描述
在这里插入图片描述

源一,源二,源三是student2中的数据

2.INNER JOIN:内连接

INNER JOIN 关键字在表中存在至少⼀个匹配时返回行。

SELECT column_name(s) FROM table1 JOIN table2 ON table1.column_name=table2.column_name;

在这里插入图片描述

3.LEFT JOIN:左连接

LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL。

SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name;

为了使展示效果更加明显,我在添加了一些数据及排序

# 学生表中添加:
insert into `student` 
     (`name`, `gender`, `city`, `description`, `birthday`, `money`, `only_child`) values 
     ('源一', '男', '北京', '班⻓', '1997/10/1', rand() * 100, True), 
     ('源二', '⼥', '上海', NULL, '1995/3/2', rand() * 100, True);  

# 成绩表中添加:
insert into score (`id`,`math`, `english`) values (20, 49, 71), (21, 62, 66.7); 

在这里插入图片描述

4.RIGHT JOIN:右连接

RIGHT JOIN 关键字从右表(table2)返回所有的行,即使左表(table1)中没有匹配。如果左表中没有匹配,则结果为 NULL。

SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name;

在这里插入图片描述

5.FULL JOIN:全连接

FULL JOIN 的连接方式是只要左表(table1)和右表(table2)其中⼀个表中存在匹配,则返回行。相当于结合了 LEFT JOIN 和 RIGHT JOIN 的结果。
特别注意: MySQL 并不⽀持 full join

SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name;

6.子查询

查询的语句中还有⼀个查询:子查询

select name from student where id in (select id from score where math > 80);

在这里插入图片描述

五.视图表

1.视图表的特点

1.视图是数据的特定子集,是从其他表里提取出数据而形成的虚拟表,或者说临时表。
2.创建视图表依赖⼀个查询。
3.视图是永远不会自己消失的除非手动删除它。
4.视图有时会对提高效率有帮助。临时表不会对性能有帮助,是资源消耗者。
5.视图⼀般随该数据库存放在⼀起,临时表永远都是在 tempdb 里的。
6.视图适合于多表连接浏览时使用;不适合增、删、改,这样可以提高执行效率。
7.⼀般视图表的名称以 v_ 为前缀,用来与正常表进行区分。
8.对原表的修改会影响到视图中的数据。

2.创建视图

 create view 视图名 as 查询语句

例:

create view v_stu as select student.*,score.math,score.english from student left join score on student.id=score.id;

创建好后,可以使用show tables;进行查看:
在这里插入图片描述
也可以通过select *from v_stu;查看表数据
在这里插入图片描述

  • 18
    点赞
  • 173
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值