简体   繁体   English

左外部联接到生成的表?

[英]Left outer join to a generated table?

Am I on completely the wrong tack ? 我的做法完全错误吗? I want to do a left outer join to a query generated from 2 tables , but i keep getting errors. 我想对从2个表生成的查询进行左外部联接,但我不断出错。 Do I need a different approach? 我需要其他方法吗?

t1: 1:

ID, Surname,Firstname

t2: 2:

ID,JobNo,Confirmed

I have the following query: 我有以下查询:

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

I want to add: 我要添加:

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

Can I do a left outer join to a query generated from 2 tables ? 我可以对从2个表生成的查询进行left outer join联接吗?

In order to use the t1 and t2 in the left outer join that you want to add you need to join them with the first tables, you can't reference them directly in the left outer join you, Something like the following: 为了在要添加的左外部t2中使用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

So, based in your tables' structure, define the conditions of the two joins with t1 and t2 with other tables. 因此,根据表的结构,用t1t2其他表的两个联接的条件。

Here is the an example of a left join to a sub query. 这是左连接子查询的示例。 This might be what you are looking for. 这可能是您要寻找的。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM