mysql_连表查询
Posted 手可摘星辰。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql_连表查询相关的知识,希望对你有一定的参考价值。
1.内连接(只取两张表的共同部分)
# 将表employee 和 department 的相同部分根据employee表中的dep_id字段和department表中的id字段连接起来。 select * from employee inner join department on employee.dep_id = department.id;
2.左连接(在内连接的基础上保留左边表的记录)
select * from employee left join department on employee.dep_id = department.id;
3.右连接(在内连接的基础上保留右边表的记录)
select * from employee right join department on employee.dep_id = department.id;
4.全外连接(保留左右两表没有对应关系的记录)
# 将左连接和右连接查出来的记录和在一起,再去重~ select * from employee left join department on employee.dep_id = department.id union select * from employee right join department on employee.dep_id = department.id;
#示例1:以内连接的方式查询employee和department表,并且employee表中的age字段值必须大于25,即找出年龄大于25岁的员工以及员工所在的部门 select employee.name,department.name from employee inner join department on employee.dep_id = department.id where age > 25; #示例2:以内连接的方式查询employee和department表,并且以age字段的升序方式显示 select * from employee inner join department on employee.dep_id = department.id order by age asc;
以上是关于mysql_连表查询的主要内容,如果未能解决你的问题,请参考以下文章