在对数据库进行多表查询时,我们需要将多个数据表联结起来,多表的联结主要有以下几种类型:
1.左联结(left join):联结结果保留左表中的全部数据
2.右联结(right join):联结结果保留右表中的全部数据
3.内联结(inner join):取两个表中的共同数据
使用好以上三种联结类型,我们将能够写出许多精彩复杂的查询操作。
以下有几种情况(阴影部分为我们需要查询的内容),其图示及sql语句写法如下:
select ... from table1 as a
inner join table2 as b
on a.属性列名 = b.属性列名;
select ... from table1 as a
left join table2 as b
on a.属性列名 = b.属性列;
select ... from table1 as a
left join table2 as b
on a.属性列名 = b.属性列名
where b.属性列名 = null;
select ... from table1 as a
right join table2 as b
on a.属性列名 = b.属性列名;
select ... from table1 as a
right join table2 as b
on a.属性列名 = b.属性列名
where a.属性列名 = null;
select ... from table1 as a
full outer join table2 as b
on a.属性列名 = b.属性列名;
select ... from table1 as a
full outer join table2 as b
on a.属性列名 = b.属性列名
where a.属性列名=null or b.属性列名=null;
标签:table2,...,join,数据库,查询,多表,列名,select,属性
From: https://www.cnblogs.com/fkaa/p/18240147