[原创]Mysql链接多个表

主要包括以下几种情况:

1、用逗号连接: 

SELECT *  
FROM   employee,department 
WHERE  employee.DepartmentID = department.DepartmentID  
这个时候默认是等价于内连接,即等价于:
SELECT *
FROM   employee 
INNER JOIN department 
ON employee.DepartmentID = department.DepartmentID

2、左连接(left join):

 首先在左边列出左边表的所有行,然后右边表的行如果符合链接条件就把相应数据放在结果表的右边,如果不符合就舍弃,而对于左边表中没有对应的右边表的记录的行,那么就在右边相应字段写上NULL;

3、右连接(right join):

  和上面左连接类似。

4、内连接(inner join):

  只把符合链接要求的左右表所对应的行列出来。

5、全连接(full join):

  把左右两个表所有的行都保留下来,其中符合相应的记录放在一行,对于没有对应上的记录,在相应自字段写上NULL;

  其中mysql不支持full join。

6、Join:

  如果join的左边没有诸如left、right或者inner这样的关键字时,缺省的是内连接。

7、三表关联查询:

  以left join 为列:

   select username,psw,gname,tel

   from (t1 left join t2 on t1.t1_id=t2.t1_id)

  left join t3

  on  t1.t1_id=t3.t1_id

  而不应该想当然的写为:

  select

  username,psw,gname,tel

  from t1 left join t2 left join t3

  on t1.t1_id=t2.t1_id and t1.t1_id=t3.t1_id;

参考:

http://blog.csdn.net/huanghanqian/article/details/52847835

http://blog.csdn.net/superbfly/article/details/12943255

http://www.w3school.com.cn/sql/sql_join_inner.asp

原文地址:https://www.cnblogs.com/lordcheng/p/7467564.html