云计算学习100天-第48天-MySQL数据库学习4
目录
连接查询——多表查询
内连接:
select 表头名 from 表1 as 别名 inner join 表2 as 别名 on 连接条件 [where 筛选条件]
等值连接 使用相等判断做连接条件,表里必须有存储相同数据的表头
非等值连接 不使用相等判断做连接条件,表里没有存储相同数据的表头
外连接:
左连接——左表表头记录全显示,右表表头只显示与条件匹配的记录,右表比左表少的记录使用NULL匹配
select 表头名 from 表1 as 别名 left join 表2 as 别名 on 连接条件 [where 筛选条件]
右连接——右表表头记录全显示,左表表头只显示与条件匹配的记录,左表比右表少的记录使用NULL匹配
select 表头名 from 表1 as 别名 right join 表2 as 别名 on 连接条件 [where 筛选条件]
全外连接: 联合查询
(select 语句) UNION (select 语句); #去重
(select 语句) UNION ALL (select 语句); #不去重
嵌套查询
select之后,把查询结果当表头使用
select 别名2.字段,(select语句)as 别名1 from 表名 as 别名2
from之后,把查询结果当表使用
select 字段 from (select语句)as 别名 where 条件
where之后,把查询结果作为查询条件
select 字段 from 表名 where 字段 in (select 语句);
having之后,把查询结果作为过滤条件
select 聚合条件(字段) from 表名
group by 字段
having 聚合条件(字段) < (
select 聚合条件(字段) from 表名 where 字段=(select 语句)
);
CROSS JOIN(交叉连接/笛卡尔积连接)
显式写法(推荐写法):
select * from 表1 cross join 表2
隐式写法:
select * from 表1,表2
更多推荐



所有评论(0)