I have a client,clientcontacts and contactphones tables.
each client may have many contacts, each contact may have many phones.
Some of the clients defined as sellers, meaning that sellerid is FK to clientid
I want to write a query where I give the clientid and it returns the seller contacts with phone (if any exists)
example:
clients
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
if I give clientid=3 The out put should be the contacts & phones of clientid=1 beacuse 1 is the seller of 3 as follows:
Sara 120
Sara 121
Sara 127
Rebeca
i tried the following:
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
this gives:
ERROR: syntax error at or near "using"
How can I do this query?
If possible without WITH it is preffered.
If someone can also explain what excaly is the problem with my query that could be great... I don't understand why the USING is not working here.