MySQL知识要点7--高级查询

高级查询

主要分为:关联查询子查询联合查询

关联查询

主要包括内连接、外连接、自然连接、自连接

 1. 内连接
 	通用列字段名称必须一致,去除重复字段
 	关联表中出现的字段值最终才能出现在结果集中
 	内连接与连接顺序无关,没有主从表之分
 	select name, score from student, class where student.class_name = class.class_name;
 	select * from student inner join class on student.class_name = class.class_name;
 	select * from student inner join class using(class_name); 
 2. 外连接
 	外连接有主从表之分,与连接循序有关
 	依次遍历主表中记录,与从表中记录进行匹配,如果匹配到则连接展示,否则以null填充
 	select * from student left join class on student_name=class_name;# 全部展示左边student内的数据
 	select * from student right join class on student_name=class_name;# 全部展示右边student内的数据
 	select * from student outer join class on student_name=class_name;# 只显示匹配到的两表数据
子查询

主要包括:单行子查询、多行子查询等

	# 单行子查询
	select * from student where score>(select score from student where name='张三');
	# 多行子查询
	select * from student where score in (select score from student where class_id=3) and name like '张%';
联合查询

union 将结果进行整合到一个表中
select name from student_score
union
select name from student_name;

猜你喜欢

转载自blog.csdn.net/qq_31307291/article/details/87879841