使用FULL JOIN连接两个表

时间:2014-02-03 05:45:54

标签: mysql sql join

我有两张表master1master2

insert into master1 
(name,address,books,laptop,id,college) values
(ratan,mg road,rajbook,hp,id334,xxx)
(sham,r t nagar,malbook,ibm,id335,xxx)
(ram,shiva road,kalbook,hp,id336,xxx)
(farooq,uday road,rabook,hp,id337,xxx)
(sharukh,kr road,kambook,sony,id338,xxx)

insert into master2
(nam,lappy,idd,coll) values
(ratan,hp,877,ratan college)
(ram,sony,877,ram college)

我想:

select name, address, books, laptop, id, college, nam, lappy
  from master1 LEFT JOIN master2
 where master1.college=master2.coll

输出应为:

(name,address,books,laptop,id,college,nam,lappy
(ratan,mg road,rajbook,hp,id334,xxx,keshav,hp)
(sham,r t nagar,malbook,ibm,id335,xxx,null,null)
(ram,shiva road,kalbook,hp,id336,xxx,null.null)
(farooq,uday road,rabook,hp,id337,xxx,null,null)
(sharukh,kr road,kambook,sony,id338,xxx,ram,sony)

3 个答案:

答案 0 :(得分:0)

WHERE不正确,而您遗漏了ON条款的JOIN部分。

select name,address,books,laptop,id,college,nam,lappy
from master1
left join master2 on master1.college = master2.coll

答案 1 :(得分:0)

使用ON

SELECT name,address,books,laptop,id,college,nam,lappy
FROM master1
LEFT OUTER JOIN master2
ON master1.college = master2.coll

答案 2 :(得分:0)

Select name
      ,address
      ,books
      ,laptop
      ,id
      ,college
      ,nam
      ,lappy
From master1
LEFT JOIN master2 ON 
      master1.college=master2.coll

你的左连接错了我格式化了这个

Joins Tutorial

LEFT JOIN and RIGHT JOIN Optimization

您的输出如下

enter image description here

Working SQL Fiddle