SQL Select INNER JOIN table contacts join all phones in single related record

Asked

Viewed 57 times

0

I am facing the following dilemma: I have 3 tables (Supplier, Representative and Contacts). Tabelas

And the following query:

select *
from Fornecedor F 
  inner join Representante R as on R.id_provider = F.id 
  inner join Contato C as on C.id_provider = F.id or C.id_representative = R.id 
group by F.id

The Vendor table is the main entity. The Representative table refers to the Supplier table and the Contact table refers to the Supplier and Representative tables. That is, the Contact table and the Representative table depend on the Supplier table to exist. A Vendor may have zero or more Contacts. The Vendor table also follows this rule. In the Contact table there are identifiers to know if the number saved is cellular or fixed and the type field identifies the "owner" table of the number.

I would like to know how to make the result of the consultation return the phone numbers referring to that registration in a single record without duplicating the records for each phone number, ie, I wanted a single result return the phone and the mobile phone of the supplier and the representative without duplicate record for each phone.

  • Its application ensures that Contato and Representante reference the same supplier or may be different suppliers?

  • Yes, it does guarantee.

  • A supplier may have one or several representative one representative may have only one supplier one supplier may have zero or multiple contacts one representative may have zero or multiple contacts

  • Assess whether the aggregation function GROUP_CONCAT for the phone field meets your needs.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.