左外连接到生成的表?

时间:2012-01-05 00:25:26

标签: mysql join outer-join

我完全错了吗? 我想对从2个表生成的查询执行左外连接,但我不断收到错误。我需要一种不同的方法吗?

T1:

ID, Surname,Firstname

T2:

ID,JobNo,Confirmed

我有以下查询:

SELECT JobNo AS N, StaffID AS P, Confirmed as C, 
       FirstName AS F,Surname AS S 
FROM gigs_players, Players 
WHERE t1.StaffID=t2.StaffID AND JobNo="2" 
      AND (`Confirmed` IS NULL OR Confirmed ='Y' ) 
ORDER BY Instrument,Surname

我想补充一下:

LEFT OUTER JOIN contacted (ON t1.StaffID=contact.ID AND t2.JobNo=contact.JobNo)"

我可以对从2个表生成的查询执行left outer join吗?

2 个答案:

答案 0 :(得分:0)

要使用要添加的左外连接中的t1t2,您需要将它们与第一个表连接,您不能直接在左外部引用它们加入你,如下所示:

SELECT JobNo AS N, StaffID AS P, Confirmed as C, 
   FirstName AS F,Surname AS S 
FROM gigs_players, Players
Inner join t1 on ...
Inner join t2 on ...
LEFT OUTER JOIN contacted c 
     on t1.StaffID=c.ID AND t2.JobNo = c.JobNo
WHERE t1.StaffID=t2.StaffID AND JobNo="2" 
    AND (`Confirmed` IS NULL OR Confirmed ='Y' ) 
ORDER BY Instrument,Surname

因此,根据表格的结构,使用t1t2与其他表格定义两个联接的条件。

答案 1 :(得分:0)

以下是对子查询的左连接示例。这可能就是你要找的东西。

select 
parts.id, 
min(inv2.id) as nextFIFOitemid 
from test.parts 
left join 
( select 
  inventory.id, 
  coalesce(parts.id, 1) as partid 
  from test.inventory 
  left join test.parts 
  on (parts.id = inventory.partid) 
) inv2 
on (parts.id = inv2.partid) 
group by parts.id;