我有一个client
,clientcontacts
和contactphones
表。从postgresql中的3个表中获取行
每个客户端可能有很多联系人,每个联系人可能有多个电话。
一些定义为卖家的客户,这意味着sellerid
是FK到clientid
我想在这里我给clientid
编写一个查询并返回与手机销售商的联系人(如果存在的话)
例如:
客户
clientid name sellerid
1 jack
2 jeff
3 robin 1
clientcontacts
contactid clientid name
1 1 Robert
2 1 Magen
3 3 Sara
4 3 Rebeca
contactphones
contactphoneid contactid phone
1 1 00522
2 1 15541
3 1 555841
4 3 120
5 3 121
6 3 127
如果我给的clientid = 3 的出放应在接触&手机客户端ID = 1怎么一回事,因为1是3卖方如下:
Sara 120
Sara 121
Sara 127
Rebeca
我试过以下内容:
With seller (select sellerid from clients where clientid=INPUT)
select name,phone
from clientcontacts
using seller
left join contactphones on(clientcontacts.contactid=contactphones.contactid)
where clientcontacts.clientid=seller.sellerid
这给:
ERROR: syntax error at or near "using"
我怎么能这样做的查询? 如果可能,没有WITH
它是优先考虑的。
如果有人也可以解释什么excaly是我的查询问题,可能是伟大的......我不明白为什么USING
不在这里工作。
你试过简单的JOIN吗? – jarlh
是的......不能使它工作......我无法得到所需的结果,因为你不能这样做:clientid =输入和clientid = resellerid –