多对多
select
*
from A,B
where A.x=B.x
and A.age=18;
内连接
-- 1、等值连接
select
*
from A
join B on A.x=B.x
where A.age=18;
-- 2、非等值连接
select
*
from emp e
join grade g on e.sal between g.min and g.max;
外连接
-- 1、左外链接
-- A表全部 + B中和A关联的记录,在B找不到和A关联的记录的则用null填充
select
*
from A
left join B on A.x=B.x
where A.age=18;
-- 2、右外链接
-- B表全部 + A中和B关联的记录,在A找不到和A关联的记录的则用null填充
select
*
from A
right join B on A.x=B.x
where A.age=18;
-- 一级部门的parent_id=0,所以它的上级部门是关联不上的,所以使用left join
select
t1.id,
t1.name '部门名称'
ifnull(t2.name,'无') '上级部门名称'
from dept t1
left join dept t2 on (t1.parent_id=t2.id)
select
t1.username,
t2.dept_name
form t_user t1
join dept t2 on t1.dept_id = t2.id
where t2.name='xxx'
-- 1、left jon
-- left join会返回左表的全部数据,此时在on中添加左表的条件会失效
-- 若要对左表进行条件过滤则只能放在where后面
select
t1.username,
t2.dept_name
form t_user t1
left join dept t2 on (t1.dept_id = t2.id and t1.username='xxx')
-- 2、right join
-- right join会返回右表的全部数据,此时在on中添加右表的条件会失效
-- 若要对右表进行条件过滤则只能放在where后面
update table_1 t1,table_2 t2
set t1.column = t2.column
where t1.id = t2.pid
update t_user user,t_dept dept
set dept.name=concat(user.username,substring(dept.name,4))
where user.dept_id=dept.id