Mysql连接查询

连接查询

含义:又称多表查询,当查询的字段来自于多个表时,就会用到连接查询

如果没有有效的连接条件,则会发生笛卡尔积现象:表1 有n行,表2有m行,结果=n*m行

正确的方法应该是

select name,boyName 
from boys,beauty
where beauty.boyfriend_id = boys.id;

连接查询的分类

按年代分类

sql92标准(仅仅支持内连接)

sql99标准(推荐)都支持

按功能分类

内连接:

            等值连接

            非等值连接

             自连接

外连接:

            左外连接

            右外连接

交叉连接

一、sql92标准

1.等值连接

查询女神名和对应的男神名

select name,boyName 
from boys,beauty
where beauty.boyfriend_id = boys.id;

两个表的顺序可以互换

查询员工名和对应的部门名

select work_name,department_name
from employees,departments
where employees.department_id = departments.department_id;

可以直接给表起别名

select work_name,department_name
from employees as e,departments as d
where e.department_id = d.department_id;

查询有奖金的员工名、部门名

select work_name,department_name
from employ e,department d
where e.department_id = d.department_id
and e.commission_pct is not null;

可以加分组吗

查询每个城市的部门个数

select count(*),city
from departments d,locations l
where d.location_id = l.location_id
group by city;

查询有奖金的每个部门的部门名和部门的领导编号和该部门的最低工资

select d.admin_id,min(salary),department_name
from departments d,employees e
where d.department_id = e.department_id
and commission_pct is not null
gruop by department_name,d.admin_id;
//两个表都有admin_id 所以要用别名

可以实现三表连接吗?

查询员工名、部门名和所在的城市

select work_name,department_name,city
from employees e,departments d,locations l
where e.department_id = d.department_id and l.location_id = d.location_id

2.非等值连接

查询员工的员工和工资级别

select salary,grade_level
from employees e,job_grades g
where salary between g.lowest_sal and g.highest_sal;

3.自连接(自己与自己连接)

查询员工名和上级的名称

select a.last_name,b.manager_id
from employees a,employees b
where a.manager_id = b.employee_id;

4.自然连接

select 属性名
from 表1 别名 natural join 表2

二、sql99语法

连接查询语法可以为

select 查询列表
from 表1 别名 连接类型
join 表2 别名
on 连接条件
where 筛选条件
group by分组
having 筛选条件
order by 排序列表


内连接 inner
外连接
左外 left 【outer】
右外 right 【outer】
全外 full 【outer】
交叉连接 cross
【】指的是可写可不写

一、内连接

语法

select 查询列表
from 表1 别名
inner join 表2 别名
on 连接条件

等值连接

查询员工名、部门名

select last_name,department_name
from employees e
inner join departments d
on d.department_id = e.department_id;

查询员工名、部门名、工种名、并按部门名降序

select last_name,department_name,job_title
from employee e
inner join departments d on e.department_id = d.department_id
inner join jobs j on e.job_id = j.job_id
order by department_name desc;

非等值连接

查询员工级别

select salary,grade_level
from employees e
inner join job_grades g on e.salary between g.lowest_sal and g.highest_sal;

自连接也行

二、外连接

有两个表

左外连接就是以左边的表为主表,如果主表中的元组与右边表的元组没有匹配的,则主表对应元组连接上一个null

右外连接就是以右边的表为主表

全外连接就是,两边都保存null

查询没有男朋友的女神

select b.name,bo.*
from beauty b left outer join boys bo
on b.boyfriend_id = bo.id;

三、交叉连接

交叉连接就是笛卡尔积

 

 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值