数据库(关联查询 分组)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SS__FF/article/details/78514479

一.关联查询

1.内连接  inner  join

select * from emp where id >12 inner join department;

2.左连接  left join ......on

左表为基准。右表没有就为null


select * from emp where id>12 left join department  on emp.did=department.id;

3.右连接  right join .....on

右表为基准。左表没有就为null


select * from emp where id>12 right join department  on emp.did=department.id;

4.完全连接(左右表所有记录都存在,一方没有就为null)


select * from emp  full join department on emp.did=department.id;



二.分组

1.分组时注意查询字段,只能是统计函数(count,max,min ,avg)

或者是被分组的字段

2.having使用时,后面跟的条件必须是查询的列(字段)之一

select id ,count(name) from emp group by id ;

select id,avg(salary) from emp group by id having salary>12100;、



猜你喜欢

转载自blog.csdn.net/SS__FF/article/details/78514479