msql-多表查询

链接查询

苗卡尔积查询

mysql> SELECT * FROM employee,department;
形成M×N的形式,依次匹配

内联接

用join连接两张表,on 表示限制条件
join只能主表和子表相交的部分

select * from employee inner join department 
on employee.dept_id = department.dept_id;```

## 外联接

### 左联接
```elect * from employee left join department on employee.dept_id = department.dept_id;```
在内联接的基础上增加左表有,右表没有的内容

### 右联接

```select * from employee RIGHT JOIN department on employee.dept_id = department.dept_id;```
同理可得
### 全外联接

在内联接的基础上增加左表有,右表没有和左表没有,右表有的内容

select * from employee RIGHT JOIN department
on employee.dept_id = department.dept_id
UNION
select * from employee LEFT JOIN department
on employee.dept_id = department.dept_id;


union 和 union all 的区别在于:union 会去除相同的内容

你可能感兴趣的:(msql-多表查询)