MySQL查询语言

MySQL查询语言

查询table1的所有信息
select * from table1;
select column1, column2,... from table1;
投影、别名
select column1 as col1, column2 as col2 from table1;

在这里插入图片描述

筛选
  • where来引入条件
-- 查询所有女学生的姓名和出生日期(筛选)
select stu_name, stu_birth from tb_student where stu_sex=0;
-- 查询所有80后学生的姓名、性别和出生日期(筛选)
select stu_name, stu_sex, stu_birth from tb_student
where stu_birth between 1980-1-1 and 1989-12-31;
-- 在查询时可以对列的值进行处理
-- 分支结构建议用(case...when 条件 then...else...end)
select stu_name as 姓名, 
case stu_sex when 1 then '男' else '女' end as 性别, 
stu_birth as 生日
from tb_student where stu_birth between 1980-1-1 and 1989-12-31;
-- MySQL方言:if()函数(其他数据库可能不兼容)---> Oracle方言decode()函数
select stu_name as 姓名, 
if(stu_sex, '男', '女') as 性别, 
stu_birth as 生日
from tb_student where stu_birth between 1980-1-1 and 1989-12-31;
通配符
  • 通配符(wild card) - %:匹配零个或任意多个字符;_:精确匹配一个字符。
-- 查询姓”杨“的学生姓名和性别(模糊)
select stu_name, stu_sex from tb_student where stu_name like '杨%';
-- 查询姓”杨“名字两个字的学生姓名和性别(模糊)
select stu_name, stu_sex from tb_student where stu_name like '杨_';
-- 查询姓”杨“名字三个字的学生姓名和性别(模糊)
select stu_name, stu_sex from tb_student where stu_name like '杨__';
-- 查询名字中有”不“字或“嫣”字的学生的姓名(模糊)
select stu_name from tb_student 
where stu_name like '%不%' or stu_name like '%嫣%';
-- 基于正则表达式的模糊查询
select stu_name from tb_student 
where stu_name regexp '.*不.*' or stu_name like '.*嫣.*';
  • union和union all
    union: 去重;union all: 不去重
select stu_name from tb_student where stu_name like '%不%' 
union
select stu_name from tb_student where stu_name like '%嫣%';
空值的处理
  • 空值(null)做任何运算结果也是产生空值(null),null相当于条件不成立(什么都查不到)
-- 查询没有录入家庭住址的学生姓名(空值)
select stu_name from tb_student where stu_sddr is null;
-- 查询录入了家庭住址的学生姓名(空值)
select stu_name from tb_student where stu_sddr is not null;
去重
  • 使用distinct
-- 查询学生选课的所有日期(去重)
select distinct sel_date from tb_record;
-- 查询学生的家庭住址(去重)
-- stu_addr有空值
select distinct stu_addr from tb_student where stu_addr is not null;
排序(order by)
-- 查询男学生的姓名和生日按年龄从大到小排列(排序)
-- 升序(从小到大)---> ascending
select stu_name, stu_birth from tb_student
where stu_sex=1
order by stu_birth asc;

-- 降序(从大到小)---> descending
-- curdate ---> 获取当前日期
-- datediff ---> 计算时间差(以天为单位)
-- floor / ceil ---> 向下/上取整
select stu_name, stu_birth as 生日
	floor(datediff(curdate(), stu_birth)/365) as 年龄 
from tb_student where stu_sex=1
order by stu_birth desc;
聚合函数
-- 查询年龄最大的学生的出生日期(聚合函数)
select min(stu_birth) from tb_student;

-- 查询年龄最小的学生的出生日期(聚合函数)
select max(stu_birth) from tb_student;

-- 查询编号为1111的课程考试成绩的最高分
select max(score) from tb_record where cou_id=1111;

-- 查询学号为1001的学生考试成绩的最低分
select min(score) from tb_record where stu_id=1001;

-- 查询学号为1001的学生考试成绩的平均分
select avg(score) from tb_record where stu_id=1001;

-- 查询学号为1001的学生考试成绩的平均分,如果有null值,null值算0分
-- ifnull ---> 如果遇到null(空值),将其替换为指定的值
select avg(ifnull(score,0)) from tb_record where stu_id=1001;

-- 查询学号为1001的学生考试成绩的标准差
select round(std(score),4) from tb_record where stu_id=1001;
分组和聚合函数
-- 查询男女学生的人数(分组和聚合函数)
select
	if(stu_sex, '男', '女') as 性别
	count(*) as 人数
from tb_student group by stu_sex;

-- 查询每个学院男女学生人数
select
	col_id as 学院编号,
	if(stu_sex, '男', '女') as 性别,
	count(*) as 人数
from tb_student group by col_id, stu_sex;

-- 查询每个学生的学号和平均成绩(分组和聚合函数)
select 
	stu_id as 学号, 
	round(avg(score),2) as 平均分
from tb_record group by stu_id;
	
-- 查询平均成绩大于等于90分的学生的学号和平均成绩
select 
	stu_id as 学号, 
	round(avg(score),2) as 平均分
from tb_record group by stu_id
having 平均分>=90;

-- 查询1111、2222、3333三门课程平均成绩大于等于90分的学生的学号和平均成绩
-- 分组以前的数据筛选使用where子句,分组以后的数据筛选使用having子句
select 
	stu_id as 学号, 
	round(avg(score),2) as 平均分
from tb_record where cou_id in (1111,2222,3333)
group by stu_id having 平均分>=90;
子查询和连接查询
-- 查询年龄最大的学生的姓名(子查询)
-- 嵌套查询:把一个select的结果作为另一个select的一部分来使用
-- 嵌套查询通常也称之为子查询,在查询语句中有两个或多个select
select stu_name from tb_student 
	where stu_birth=(
		select min(stu_birth) from tb_student
	);

-- 查询年龄最大的学生姓名和年龄(子查询+运算)
-- 获取当前时间:curdate(), 求差值:datediff()
select stu_name, 
	floor(datediff(curdate(),stu_birth)/365) as 年龄
from tb_student where stu_birth=(
		select min(stu_birth) from tb_student
);

-- 查询选了两门以上的课程的学生姓名(子查询/分组条件/集合运算)
select stu_name from tb_student
where stu_id in (
	select stu_id from tb_record group stu_id where count(*)>2);

-- 查询学生的姓名、生日和所在学院名称
select stu_name, stu_birth, col_name
from tb_student t1 
inner join tb_coolege t2 on t1.col_id=t2.col_id;

-- 查询学生姓名、课程名称以及成绩(连接查询/联结查询)
-- limit n: 限制取n条;limit n offset m: 跳过m条,取后面的n条
select stu_name, cou_name, score
from tb_record t1 
inner join tb_student t2 on t1.stu_id=t2.stu_id
inner join tb_course t3 on t1.stu_id=t3.stu_id
where score is not null 
-- order by score desc limit 10,5
order by score desc limit 5 offset 10;

-- 查询选课学生的姓名和平均成绩(子查询和连接查询)
select stu_name, avg_score
from tb_student t1 inner join
    (
		-- 结果是形似表
		select stu_id, round(avg(score),1) as avg_score
		from tb_record group by stu_id
	) t2 on t1.stu_id=t2.stu_id;

-- 查询学生的姓名和选课的数量
select stu_name, total 
from tb_student t1 inner join(
	select stu_id, count(*) as total from tb_record group by stu_id
) t2 on t1.stu_id=t2.stu_id;

-- 查询每个学生的姓名和选课数量(左外连接和子查询)
-- 内连接:查询左右两表满足连接条件的数据。
-- 外连接
-- 左外连接:确保左表(现在join前面的表)中的所有记录都能查出来,不满足连接条件的补充null。
-- 右外连接:确保右表(现在join后面的表)中的所有记录都能查出来,不满足连接条件的补充null。
-- 全外连接:确保左表和右表中的所有记录都能查出来,不满足连接条件的补充null。

-- 左外连接
select stu_name, ifnull(total, 0) as 选课数量 
from tb_student t1 left outer join(
	select stu_id, count(*) as total from tb_record group by stu_id
) t2 on t1.stu_id=t2.stu_id;

-- 删除tb_record表的外键约束
alter tb_record drop foreign key fk_record_stu_id;
alter tb_record drop foreign key fk_record_cou_id;

-- 给tb_record表加两条记录,学号5566在学生表没有对应的记录
insert into tb_record 
values
	(default, 5566, 1111, '2019-09-02', 80),
    (default, 5566, 2222, '2019-09-02', 70);

-- 右外连接
select t1.stu_id, stu_name, t2.stu_id, total as 选课数量 
from tb_student t1 right outer join(
	select stu_id, count(*) as total from tb_record group by stu_id
) t2 on t1.stu_id=t2.stu_id;

-- MySQL不支持全外连接
-- 可以通过左外连接与右外连接求并集运算得到全外连接的结果
select t1.stu_id, stu_name, t2.stu_id, total as total 
from tb_student t1 left outer join (
	select stu_id, count(*) as total from tb_record 
	group by stu_id
) t2 on t1.stu_id=t2.stu_id
union 
select t1.stu_id, stu_name, t2.stu_id, total as total 
from tb_student t1 right outer join (
	select stu_id, count(*) as total from tb_record 
	group by stu_id
) t2 on t1.stu_id=t2.stu_id;
  • 囊括所有数据库查询的方法
    请添加图片描述
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: MySQL 递归查询语言主要是通过使用 `WITH RECURSIVE` 关键字来实现的,类似于其他数据库管理系统中的 `WITH RECURSIVE` 关键字。 下面是一个使用 MySQL 递归查询语言的例子,假设我们有一个格 `employee` 包含员工的 ID、姓名和上级 ID: ```sql CREATE TABLE `employee` ( `id` INT NOT NULL, `name` VARCHAR(45) NOT NULL, `manager_id` INT NULL, PRIMARY KEY (`id`) ); INSERT INTO `employee` (`id`, `name`, `manager_id`) VALUES (1, 'Alice', NULL), (2, 'Bob', 1), (3, 'Charlie', 2), (4, 'David', 2), (5, 'Eve', 1), (6, 'Frank', 5), (7, 'Grace', 5), (8, 'Henry', 7); ``` 现在我们想要查询每个员工的直接上级以及所有上级的姓名,可以使用递归查询语言来实现: ```sql WITH RECURSIVE cte (id, name, manager_id, level) AS ( SELECT id, name, manager_id, 0 FROM employee WHERE id = 6 -- 员工 ID UNION ALL SELECT e.id, e.name, e.manager_id, c.level + 1 FROM employee e JOIN cte c ON e.id = c.manager_id ) SELECT CONCAT(REPEAT(' ', level), name) AS name, level FROM cte ORDER BY level; ``` 这个查询语句会输出以下结果: ``` Frank 0 Eve 1 Grace 2 Henry 3 Alice 1 ``` 这个结果示 Frank 的直接上级是 Eve,Eve 的直接上级是 Alice,而 Grace 和 Henry 是 Eve 的上级,Alice 没有上级。 ### 回答2: MySQL递归查询语言是指在MySQL数据库中使用递归查询来实现对层次结构数据的查询。递归查询可以用于处理带有父子关系的数据,例如组织机构、分类目录等。 MySQL递归查询语言的实现方式是通过使用WITH RECURSIVE子句来定义递归查询。这个子句包含两部分:递归部分和终止条件部分。递归部分定义了每一次递归查询时如何从上一次的结果中获取下一级的数据,而终止条件部分定义了递归查询何时停止。 在递归查询中,通过使用UNION操作符将递归部分和终止条件部分连接在一起。递归部分的查询语句中使用了上一次递归查询的结果作为输入,并且在结果中筛选出下一级的数据。终止条件部分的查询语句用于获取最顶层的数据。 递归查询语言的基本语法如下: ``` WITH RECURSIVE cte_name (column1, column2, ...) AS ( SELECT initial_query UNION ALL SELECT recursive_query ) SELECT * FROM cte_name; ``` 递归查询语言的应用场景很多,例如可以用来查询组织机构的层级结构、获取分类目录的嵌套关系等。通过递归查询,可以轻松地遍历和操作具有层次结构的数据。 ### 回答3: MySQL并没有专门的递归查询语言。但是可以通过使用存储过程和临时来实现递归查询。 在MySQL中,可以通过编写存储过程来实现递归查询。存储过程是一种预先编译的SQL语句集合,可以在MySQL数据库上进行执行。在存储过程中,可以使用循环和条件语句来进行递归查询。通过逐步迭代和条件判断,可以实现对具有层级结构的数据进行递归查询。 此外,临时也是实现递归查询的一种方法。临时是一种临时存储数据的,它只存在于当前会话中,并在会话结束后自动被删除。通过创建多个临时和通过循环插入数据到临时,可以模拟递归查询的过程。 总的来说,虽然MySQL并没有原生的递归查询语言,但是可以通过存储过程和临时等方法来实现递归查询。递归查询在处理具有层级结构的数据时非常有用,可以实现对树形结构数据的遍历和查询。但是需要注意的是,递归查询可能会导致性能问题,所以在使用时要谨慎考虑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值