版权声明:本文为博主原创文章,未经博主允许不得转载☺ https://blog.csdn.net/u014571132/article/details/89785284
创建一张tb_student表用于之后测试,主键是id,加上name,score字段
CREATE TABLE `tb_student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`score` int(11) NOT NULL,
PRIMARY KEY (`id`)
)
以下是测试sql,感觉把sql放在一起看比较容易理解
select type | 解释 | 测试sql |
simple | 简单的select | select * from tb_student |
primary | 需要union或者子查询 | select (select name from tb_student where id =1) from tb_student |
union | union操作 | select * from tb_student union select * from tb_student |
dependent union | 查询与外部相关(mysql优化器会将in优化成exists) | select * from tb_student where id in(select id from tb_student union select id from tb_student) select * from tb_student a where EXISTS (select 1 from tb_student where id = a.id union select id from tb_student where id = a.id) |
union result | union结果集 | select * from tb_student union select * from tb_student |
subquery | 除了from包含的子查询 | select (select name from tb_student where id =1) from tb_student |
depend subquery | 类似depend union | select (select name from test.tb_student a where a.id=b.id) from test.tb_student b |
derived | 派生表 | select * from (select * from tb_student) t |