2019-07-14

联合查询

基本语法:
select 语句1
union [union 选项]
select 语句2……

union选项:()
all:保留所有,不管重复
distinct:去重,默认

--同一张表
--联合查询多张表查询 去重
select * from my_class 
union
select * from my_class;
--不去重
select * from my_class 
union all --不去重
select * from my_class;

--不相同的表查询的内容也不相同
select id,c_name,room from my_class 
union all --不去重
select name,number,id from my_student;

下面我们做一个小练习

--男生升序,女生降序
(select * from my_student where sex='男' order by age asc limit 99999999)
union
(select * from my_student where sex='女' order by age desc limit 99999999)

按位置分类

from子查询

where子查询

exists 子查询

按结果分类

标量子查询

--一行一列
select * from my_student where c_id =(select id from my_class where c_name="python1809");

l列子查询

select * from my_student where c_id 
in--多个数据源
(select id from my_class);

行子查询

select * from my_student where age =(elect max(age)  from my_student)
ang
height =(select max(age) ,max(heiht) from my_student);


select * from my_student where (age,height) = (selet max (max),selet max (height)
from my_student);

select * from my_student  order by age besc,height desc limit 1;

表子查询

select * from my_student where
exists (select * from my_class where id=1);



select * from my_student where
exists (select * from my_class where id=2);
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. SQL 简介 SQL 的目标 理想情况下,数据库语言应允许用户: 建立数据库和关系结构 完成基本数据管理任务...
    板蓝根plank阅读 2,385评论 0 11
  • 作者:烨竹 数据库的基本操作 登陆MySQL数据库服务:mysql -h服务器地址 -P端口号 -u用户名 ...
    DragonRat阅读 1,395评论 0 7
  • 查找和替换的不同玩法! 打卡第四天!每天学习30minExcel成了日常必做的事情,可以在每天短短30min时间学...
    杨爱鑫阅读 252评论 0 2
  • 你能想象,这俩人都去过西藏吗? 记得去年某次回老家和爷爷聊天才得知,爷爷竟然也去过西藏,我不禁震惊,是不是和爷爷沟...
    YQ筑梦阅读 188评论 4 5
  • 一个会玩的小法师在后期的伤害只能够用“可怕”来形容。 我喜欢玩阿木木,所以我一直都以为自己可以横行霸道,不管是谁我...
    黄铜刀阅读 252评论 0 2