软件测试最常用的 SQL 命令(二) | 高级 Join 多表查询

38 阅读1分钟
  • INNER JOIN:如果表中有至少一个匹配,则返回行
    • LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行
    • RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行
    • FULL JOIN:只要其中一个表中存在匹配,则返回行
    • INNER JOIN-取出一个员工的员工号、生日、部门名字
    • LEFT JOIN-取出一个员工的员工号、生日、部门名字,员工号和生日为必展示项 。
  • select e.emp_no,e.birth_date,d.dept_no,n.dept_name
  • from employees e
  • left join (dept_emp d,departments n)
  • on e.emp_no=d.emp_no and d.dept_no=n.dept_no
  • order by emp_no;

- RIGHT LEFT-将dept_emp和departments的数据合并展示,且部门表departments信息为必展示项。

select * from dept_emp d right join departments n on d.dept_no=n.dept_no order by emp_no;


- FULL JOIN-将dept_emp表和departments表的行全部合并返回  
- 注:因为 MySQL 不支持 full join,所以想要实现此功能需要使用 left join union right join 的方法:

select * from dept_emp d left JOIN departments n on d.dept_no=n.dept_no union select * from dept_emp d right join departments n on d.dept_no=n.dept_no order by emp_no limit 100;


[原文链接](https://mp.weixin.qq.com/s?__biz=MzU3NDM4ODEzMg==&mid=2247487142&idx=1&sn=f0a0f7415c422eb36d6d484b0eb3aa94&chksm=fd326c6dca45e57b43d3f00ab1db5597162a67fb0705d4653d72bc353aa7c441246ac02b4b29#rd) 

[更多技术文章](https://qrcode.ceba.ceshiren.com/link?name=article&project_id=qrcode&from=juejin&timestamp=1663233229&author=ML