左链接
以左表为基准,去右表找数据,找不到匹配,用NULL填充
select 列1,列2,列N from # 查询的列
table Aleft jointable B # A表、B表进行链接
on tableA.x列 = table B.x列 # 链接条件
where 筛选条件 # 查询筛选条件 ( where,group by ,having,order by,limit )
右链接
和左链接类似,推荐转换成左链接 B RIGHT JOIN A ----> A LEFT JOIN B
select 列1,列2,列N from # 查询的列
table Aright jointable B # A表、B表进行链接
on tableA.x列 = table B.x列 # 链接条件
where 筛选条件 # 查询筛选条件 ( where,group by ,having,order by,limit )
内链接
查询左右表,含有相同数据的行的信息,即不要左/右链接中,null 的数据,内链接是左右链接的交集
select 列1,列2,列N from # 查询的列
table Ainner jointable B # A表、B表进行链接
on tableA.x列 = table B.x列 # 链接条件
where 筛选条件 # 查询筛选条件 ( where,group by ,having,order by,limit )
多次左链接
如查询 2006 5月1日 到2006 6月1日 主课队之间的比赛信息
比赛信息表 m(mid比赛编号,hid 主队Id,gid 客队id,mres 比赛结果)
球队信息表 t
select t1.tname,mres,t2.tname,matime
from m left join t as t1
on m.hid = t1.tid
left join t as t2 # 第二次链接
on m.gid = t2.tid
where matime between '2006-05-01' and '2006-07-01';