mysql连接语句:
select table1.column,table2.column
from table1 [inner | left | right | full ] join table2 on table1.column1 = table2.column2;
inner join 表示内连接;
left join表示左外连接;
right join表示右外连接;
full join表示完全外连接;
on子句 用于指定连接条件。
注意:
如果使用from子句指定内、外连接,则必须要使用on子句指定连接条件;
如果使用(+)操作符指定外连接,则必须使用where子句指定连接条件。
例如,以下有两个表:
表 test1
表 test2
一:内连接(典型的联接运算,使用像 = 或 <> 之类的比较运算符)。包括相等联接和自然联接
select test1.*,test2.* from test1 inner join test2 where test1.id=test2.id (where 可改成on)
进一步的:
select test1.id,test1.nam,test2.class,test2.grade from test1 inner join test2 where test1.id=test2.id
二、外连接
原理:在左外连接和右外连接时都会以一张表为基表,该表的内容会全部显示,然后加上两张表匹配的内容。 如果基表的数据在另一张表没有记录。 那么在相关联的结果集行中列显示为空值(NULL)。
左外连接:联接的结果集包括 LEFT 子句中指定的左表的所有行,而不仅仅是联接列所匹配的行。如果左表的某行在右表中没有匹配行,则在相关联的结果集行中右表的所有选择列表列均为空值。
select * from test1 left join test2 on test1.id=test2.id
右外连接:右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。
select * from test1 right join test2 on test1.id=test2.id
三、全连接
联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有
匹配行,则整个结果集行包含基表的数据值。 全外连接不支持(+)这种写法。
select * from test1 full join test2